【问题标题】:NullPointerException on TextViewTextView 上的 NullPointerException
【发布时间】:2012-10-03 02:20:44
【问题描述】:

每次我想使用 setText() 更新高分文本时,都会出现空指针异常并且程序崩溃。是什么导致了这个问题?
这段代码是当我设置我的布局时,布局是使用opengl的gameView的一部分,我把highscore textview放在左上角

public void onCreate(Bundle savedInstanceState) {
    SFEngine.display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();//ambl ukuran width height layar
    super.onCreate(savedInstanceState);
    gameView = new SFGameView(this);
    gameView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    TextView textBox = new TextView(this);
    textBox.setId(1);
    textBox.setText("HIGH SCORE");
    textBox.setBackgroundColor(Color.BLUE);
    textBox.setWidth(SFEngine.display.getWidth()/2);
    textBox.setHeight(50);
    Button pauseButton = new Button(this);
    pauseButton.setText("PAUSE");
    pauseButton.setHeight(50);
    pauseButton.setWidth(SFEngine.display.getWidth()/2);
    pauseButton.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent e) {
            //pause game
            SFEngine.isPlaying = false;
            Intent i1 = new Intent(SFGames.this, pause.class);
            gameView.onPause();
            startActivityForResult(i1,0);//hrs pk result soalny mw blk lg
            return true;
        }
    });


    RelativeLayout.LayoutParams lp_pause = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams lp_hs = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    lp_hs.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    lp_pause.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp_pause.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    textBox.setLayoutParams(lp_hs);
    pauseButton.setLayoutParams(lp_pause);

    layout.addView(gameView);
    layout.addView(textBox);        
    layout.addView(pauseButton);
    setContentView(layout);

这里是 setText 代码

public boolean onTouchEvent (MotionEvent event){//buat nerima input user
    if(!SFEngine.isPlaying){
        finish();
    }
    textBox.setText("High Score :" + SFEngine.score);//here is the source of the prob
.....

错误日志:

 10-13 22:38:34.207: E/AndroidRuntime(528): FATAL EXCEPTION: main
10-13 22:38:34.207: E/AndroidRuntime(528): java.lang.NullPointerException
10-13 22:38:34.207: E/AndroidRuntime(528):  at com.starfighter.SFGames.onTouchEvent(SFGames.java:136)
10-13 22:38:34.207: E/AndroidRuntime(528):  at  android.app.Activity.dispatchTouchEvent(Activity.java:2367)

更新: 我已经更新了源代码,所以 textview 声明在 onCreate 之外,它现在似乎工作正常..

【问题讨论】:

  • 你能告诉我们你的错误日志吗?
  • 调试您的程序并确保您的 SFEngine.score 不为空。
  • 至少发布错误。

标签: android layout textview nullpointerexception


【解决方案1】:

您应该将文本视图定义为活动中的类字段,在onCreate 之外:

TextView textBox;

public void onCreate(Bundle savedInstanceState) {
    ....
    textBox = new TextView(this);
}

【讨论】:

    【解决方案2】:

    您的textBoxonCreate() 方法中的局部变量。我认为您也有一个同名的成员变量。所以你实际上用错了。

    尝试在您的onCreate() 中删除TextView

    // in onCreate
    textBox = new TextView(this);
    textBox.setId(1);
    textBox.setText("HIGH SCORE");
    

    【讨论】:

      【解决方案3】:

      将 TextView 声明放在 onCreate() 之外

      TextView textBox;
      
      public void onCreate(Bundle savedInstanceState) {
          textBox = (TextView)findViewById(id.TextBoxNameFromLayout);
      }
      

      【讨论】:

      • 在onCreate之前不能使用“this”。
      【解决方案4】:

      您的文本框应定义为全局变量。

      喜欢这个

      class MyActivity extends Activity{
          TextView textBox = null;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              ...
              textBox = new TextView(this);
      

      【讨论】:

      • 请扩展您的答案。
      • 请扩大一点,例如添加一个代码 sn-p 以显示问题所在以及如何解决。
      • 您永远不应该引用与活动之外的活动有关的任何内容。在大多数情况下,全局变量是邪恶的,将 textview 引用放入全局变量会泄漏内存。
      • 顺便说一句 - Java 中没有“全局变量”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多