我的应用程序出现问题,因为它总是崩溃,但我不确定如何修复它。任何帮助将不胜感激。我对android studio仍然缺乏经验,因此试图寻找解决方案。
这是崩溃的logcat:
10-12 02:11:37.457 7223-7223/com.fluke.kgwee.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fluke.kgwee.test, PID: 7223
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.fluke.kgwee.test/com.fluke.kgwee.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at com.fluke.kgwee.test.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
Java文件:
public class MainActivity extends AppCompatActivity {
Button buttonStart;
TextView textCounter = (TextView) findViewById(R.id.counter);
MyCountDownTimer myCountDownTimer = new MyCountDownTimer(30000, 1000, textCounter);
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
buttonStart = (Button) findViewById(R.id.start);
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myCountDownTimer.start();
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textCounter.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
textCounter.setText("done!");
}
}.start();
}
});
}
public class MyCountDownTimer extends CountDownTimer {
private TextView textCounter;
public MyCountDownTimer(long millisInFuture, long countDownInterval, TextView textCounter) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}
@Override
public void onTick(long millisUntilFinished) {
this.textCounter.setText(millisUntilFinished / 1000l + " seconds remaining : ");
}
@Override
public void onFinish() {
this.textCounter.setText("Finished");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
TextView textCounter = (TextView) findViewById(R.id.counter);
将上面的行放在setContentView之后。在设置内容视图之前,您无法访问布局的项目。