无法在单独的班级

时间:2017-02-24 17:34:29

标签: java android class timer textview

我已经创建了一个计时器并将其放在自己独立的类中。出于某种原因,当我尝试运行它时,崩溃时出现以下错误:

Java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在空对象引用上

我认为这是因为它试图从Mainactvity以外的类更新textview,但我无法弄清楚如何让它工作。

以下是代码:

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button resetButton;
TextView textTimer;
int elapsedTime;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    elapsedTime = 60;


    textTimer = (TextView) findViewById(R.id.textTimer);
    resetButton = (Button) findViewById(R.id.resetButton);

    final Button resetbttn = (Button) findViewById(R.id.resetButton);
    resetbttn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CountUp mycountup = new CountUp();
            mycountup.count();
        }
    });

}

}

计时器类:

 import android.content.Context;
 import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/ **  *由Mat于2月24日创建。  * /

public class CountUp extends MainActivity{




TextView textTimer;
Button resetButton;
int elapsedTime;
Handler h;
int RATE = 1000;
MainActivity mainact = new MainActivity();




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

textTimer = (TextView) findViewById(R.id.textTimer);
resetButton = (Button) findViewById(R.id.resetButton);



h = new Handler();

count();

final Button resetbttn = (Button) findViewById(R.id.resetButton);
resetbttn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        reset();
    }
});
}

public void reset(){
    elapsedTime = 60000;
}




public void count(){
    elapsedTime--;
    textTimer.setText(String.valueOf(elapsedTime));
    h.postDelayed(r,RATE);
    final String endcomparison = textTimer.getText().toString();
    if (endcomparison.equals("0")){
        reset();
    }
}
private Runnable r = new Runnable() {
    @Override
    public void run() {
        count();
    }
};



}

2 个答案:

答案 0 :(得分:0)

你只是在实例化一个活动,它的onCreate永远不会被调用。

CountUp mycountup = new CountUp();
mycountup.count(); // view never created so it's null

要加载视图,您需要使用startActivity等方法。

如果我正确理解你,如果你从MainActivity扩展CountUp然后在你的清单中,你将CountUp设置为你的启动活动。然后使用Java继承,您不需要重新查找视图,因为在CountUp中我们调用super.onCreate(savedInstanceState),它调用MainActivity的onCreate。

public class MainActivity extends AppCompatActivity {

Button resetButton;
TextView textTimer;
int elapsedTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  elapsedTime = 60;
  textTimer = (TextView) findViewById(R.id.textTimer);
  resetButton = (Button) findViewById(R.id.resetButton);
  }
 }

public class CountUp extends MainActivity{
  Handler h;
  int RATE = 1000;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // can access this since inheritance
    resetButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          reset();
      }
   });
    h = new Handler();
    count();
  }

//...

public void count(){
  elapsedTime--;

  // inheritance   
  textTimer.setText(String.valueOf(elapsedTime));
  h.postDelayed(r,RATE);
  final String endcomparison = textTimer.getText().toString();
  if (endcomparison.equals("0")){
      reset();
  }
 }
 //...
}

答案 1 :(得分:0)

findViewById()方法实际上在布局文件中查找视图。 当您使用

设置活动或片段的布局时
  

的setContentView(R.layout.activity_main);

findViewById()在该布局中查找这些视图。它无法动态找到随机物品。

所以在第二个活动中首先设置布局或在第一个活动中创建子类,然后尝试在textview中更新数据。