无法在活动之间调用静态方法

时间:2012-01-14 21:37:41

标签: android

我试图从另一个活动中调用静态方法来找到分数的整数:

public class HighscoreActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscore);

        TextView txtGuess = (TextView) findViewById(R.id.txtScore);
        int temp = MastermindGameActivity.getScore();
        String score = Integer.toString(temp);
        txtGuess.setText(score);

    }
}

通过调用此方法:

public static int getScore()
{
    return score;
}

为什么这不起作用?我一直在

  

01-14 21:28:54.347:ERROR / AndroidRuntime(1485):致命异常:主要   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):java.lang.RuntimeException:无法启动活动ComponentInfo {sam.android.mastermind / sam.android.mastermind.HighscoreActivity}:java.lang.NullPointerException   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.os.Handler.dispatchMessage(Handler.java:99)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.os.Looper.loop(Looper.java:123)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread.main(ActivityThread.java:3683)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at java.lang.reflect.Method.invokeNative(Native Method)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at java.lang.reflect.Method.invoke(Method.java:507)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at dalvik.system.NativeStart.main(Native Method)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):引起:java.lang.NullPointerException   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at sam.android.mastermind.HighscoreActivity.blah(HighscoreActivity.java:27)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):at sam.android.mastermind.HighscoreActivity.onCreate(HighscoreActivity.java:13)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)   01-14 21:28:54.347:ERROR / AndroidRuntime(1485):
   ......还有11个

活动在清单中定义。

1 个答案:

答案 0 :(得分:5)

试试这个..

MastermindGameActivity masterActivity = new MastermindGameActivity();

masterActivity.getScore();

您也可以尝试将其作为我推荐的意图传递。

 Intent intent=new Intent(this,Activity2.class);
 intent.putIntExtra("Score" score);

并在接收活动中将其拉出来

 Intent sender=getIntent();
     int score = sender.getExtras().getInt("Score");