java - 保存一个整数onClick,以便以后可以在android应用中调用它

我的问题是我拥有所有代码,并且没有错误(根据eclipse),但是当我尝试在我的应用程序上打开“page1”时,它冻结了,然后崩溃了,如果我获得了该页面的所有addPoints信息运行正常,您能帮我找出导致崩溃的原因吗?谢谢!
这是我的代码

package com.canadais.civics;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class page1 extends Activity implements OnClickListener 
{

    TextView Q1A1;
    TextView Q1A2;

    public static final String PREFS_NAME = "MyPrefsFile";
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    public int testScore = (settings.getInt("YourScore", 0));
    Intent page2 = new Intent (this, Page2.class);

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        Q1A1 = (TextView) findViewById(R.id.Q1A1);
        Q1A2 = (TextView) findViewById(R.id.Q1A2);
        Q1A1.setOnClickListener(this);
        Q1A2.setOnClickListener(this);
        //test = (TextView) findViewById(R.id.test);






        //test.setText(settings.getInt("YourScore", 0));


    }
    public void onClick(View v) 
    {
        switch(v.getId())
        {
        case R.id.Q1A1:
            addPoints(10);
            //Intent page2 = new Intent (this, Page2.class);
            startActivity(page2);
            break;
        case R.id.Q1A2:
            addPoints(5);
            //Intent page22 = new Intent (this, Page2.class);
            startActivity(page2);
            break;
        }   
    }



    public void addPoints(int points) 
    {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("YourScore", (testScore + points));
        editor.commit();       
    }

}

最佳答案

如果在执行startActivity()之后遇到问题,可能有多种原因。我现在可以想到两个:

AndroidManifest.xml中的

  • Missing Activity声明;
  • 要启动的Activity(您的情况下为Page2.java)中的代码不正确。

  • 为了确保您遇到哪种错误,我建议您应该学习发布错误消息,该消息出现在Android SDK附带的LogCat中,但没有出现在电话屏幕上的一个 pop 窗口中,否则,没人可以准确地找出问题所在( s)。

    https://stackoverflow.com/questions/9169718/

    相关文章:

    flash - Flash编译器不允许覆盖

    compiler-errors - Play Framework [2.0]-更改模型破坏了我的见解

    java - Eclipse 拒绝我的公共(public) void init 方法

    triggers - 触发编译错误(插入)

    java - 错误包org.python.util不存在,使用ant进行编译

    java - Apache Wink请求处理程序

    ios - 由于未捕获的异常 'NSInvalidArgumentException' 错误而终止应

    c# - 如何在ce中创建空数据库并用datagridview显示

    ios - 导出版本时也出错,找不到iOS arm-apple-darwin9-as.exe

    c++ - 算法::binary_search call中的预期主要表达式