Java 尝试在onClickListener中调用虚拟方法
每次我尝试启动我的应用程序时,这部分代码都会立即崩溃Java 尝试在onClickListener中调用虚拟方法,java,android,Java,Android,每次我尝试启动我的应用程序时,这部分代码都会立即崩溃 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!isOnline()) { // textViewTemperature.setText(Data);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isOnline()) {
// textViewTemperature.setText(Data);
Toast.makeText(getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
weather = parser.getWeatherForLocation(editText.getText() + "");
runOnUiThread(new Runnable() {
@Override
public void run() {
editText.setText(weather.getCity() + ", " + weather.getCountry()); }
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
});
这是此错误的完整日志:
12-29 10:06:36.376 3524-3524/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.piotr.pogoda, PID: 3524
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.piotr.pogoda/com.example.piotr.pogoda.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.piotr.pogoda.MainActivity.onCreate(MainActivity.java:136)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我可以看到错误,但我不明白这意味着我试图在空对象引用上使用方法。按钮不存在或什么?初始化您的
按钮
,通过findViewById(R.id.btn)
或通过编程从崩溃日志中button=new按钮(getApplicationContext())
,按钮视图为空。可能是视图初始化有一些问题。请检查代码。您需要初始化按钮
Button btn = (Button) findViewById()
也许您应该搜索如何处理NullPointerException:从崩溃日志中,按钮视图为null。也许你认为通货膨胀有一些问题。请检查代码。发布您的xml。。也可能是您没有初始化按钮idI引用的是另一个xml布局。谢谢@NikhilBorad