我一直在玩Android Studio,我正在尝试使用Chronometer小部件实现一个简单的计时器。首先我尝试了这段代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Chronometer;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Chronometer chrono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
}
public void startChrono(View view) {
chrono.start();
}
public void stopChrono(View view) {
chrono.stop();
}
}
但是当我使用它时,每当我按下“开始”按钮时,它都会使应用程序崩溃。然后我在网上找到了这个实现:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Chronometer;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Chronometer chrono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startChrono(View view) {
((Chronometer) findViewById(R.id.chronometer)).start();
}
public void stopChrono(View view) {
((Chronometer) findViewById(R.id.chronometer)).start();
}
}
这个有用,有人能指出在onCreate中创建Chronometer对象与第二个代码块之间的区别吗?
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Chronometer.start()' on a null object reference
at com.example.mykongee.testchronometer.MainActivity.startChrono(MainActivity.java:22)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
您的问题在这里:
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
在致电findViewById
之前,您无法致电setContentLayout
,因为无法查找。
你的应用程序会因为NullPointerException
而崩溃,因为chrono为空。
在你的第一个例子中,尝试交换这两行,它将与第二行相同。
setContentView(R.layout.activity_main);
chrono = (Chronometer) findViewById(R.id.chronometer);
将来,不要简单地说it didnt work
,Android提供的amazong LogCat
几乎可以告诉您问题是什么。
如果您无法自己解码错误,请发布logcat提供的堆栈跟踪。
答案 1 :(得分:0)
在第一个中,您首先尝试获取对chrono的引用,然后您将设置您的布局。问题是,你无法在没有先设置布局的情况下获得参考(无法找到那里的内容......)
假:
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
正确的:
setContentView(R.layout.activity_main);
chrono = (Chronometer) findViewById(R.id.chronometer);
在第二个例子中,您正在设置布局,然后在按钮上按下您找到参考,所以一切正常。