Java 空异常标志
我在理解为什么标记空引用时遇到了一些小问题:Java 空异常标志,java,android,Java,Android,我在理解为什么标记空引用时遇到了一些小问题: package com.example.adam.bcapplication; import android.app.Activity; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.os.Handler; import android.os.Looper; import android.support
package com.example.adam.bcapplication;
import android.app.Activity;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
//Define thread handler for the main UI thread
//define global variable types
int counter = 9;
String strcounter;
int buttoncounter = 0;
String strbcounter;
//Define the sensor Manager
SensorManager sm;
//Define the Motion Sensor objects
Sensor accelerometer;
Sensor gravity;
Sensor gyroscope;
Sensor uncalgyro;
Sensor lineaccel;
Sensor rotatevector;
Sensor sigmotion;
Sensor stepcounter;
Sensor stepdetector;
//Define the changing Motion Sensor text values
//Define the position sensor objects
Sensor gamerotatevector;
//Define the changing Position sensor objects
TextView gamerotatesense;
//may need these
final ImageButton button1 = (ImageButton) findViewById(R.id.motbut_unpressed);
final ImageView topbar2 = (ImageView) findViewById(R.id.topbarmain);
final ImageButton button2 = (ImageButton) findViewById(R.id.posbut_unpressed);
final ImageButton button3 = (ImageButton) findViewById(R.id.envbut_unpressed);
final LinearLayout bcfield = (LinearLayout) findViewById(R.id.babcocklayout);
final ImageButton arrowup = (ImageButton) findViewById(R.id.uparrow);
final ImageButton arrowdown = (ImageButton) findViewById(R.id.downarrow);
final ImageView navbar = (ImageView) findViewById(R.id.infospace);
final TextView dynamictext = (TextView) findViewById(R.id.dynamictxt);
final TextView dynamicheader = (TextView) findViewById(R.id.dynamiched);
private final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Global counter variables
counter = 9;
strcounter = Integer.toString(counter);
buttoncounter = 0;
strbcounter = Integer.toString(buttoncounter);
//Set Nav bar invisible for now
arrowup.setVisibility(View.GONE);
arrowdown.setVisibility(View.GONE);
navbar.setVisibility(View.GONE);
dynamictext.setVisibility(View.GONE);
dynamicheader.setVisibility(View.GONE);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
button1.setOnClickListener( //This is for the motion button
new ImageButton.OnClickListener() {
public void onClick(View v) {
new Thread(new button1task()).start();
dynamicheader.setText("Acceleration:");
//Now we need to register unregister the sensors and whatnot
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) { // Add check to see if we have this sensor
sm.registerListener(MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
} else if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
dynamictext.setText("Sensor not detected");
}
}
}
);
然后是如下设置的runnable:
private class button1task implements Runnable { //This is button task on new thread inside UI
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_pressed, null));
topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarmotion, null));
bcfield.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbarmotion, null));
//make sure the other two buttons are normal again
button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
//make the arrows and infospace appear
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.VISIBLE);
navbar.setVisibility(View.VISIBLE);
//Dynamic text appearance
//set the text inside infospace
dynamicheader.setVisibility(View.VISIBLE);
dynamictext.setVisibility(View.VISIBLE);
}
});
}
}
这标志着:
10-13 18:08:26.845 2345-2345/com.example.savag.babcockapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.savag.babcockapplication, PID: 2345
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.savag.babcockapplication/com.example.savag.babcockapplication.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.example.savag.babcockapplication.MainActivity.<init>(MainActivity.java:51)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
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)
10-13 18:08:31.597 2345-2345/? I/Process﹕ Sending signal. PID: 2345 SIG: 9
此错误引用顶部的第一个声明,可能是所有声明。
这就是我不明白的。。我没有正确申报吗?
我说xml中的按钮是一个特定的id。我的意思是它是一个视图或小部件,所以我不能设置为int或string,可以吗?此外,我不明白为什么这是一个空指针,我不是指向xml设计中的按钮吗
我见过好几次提到“新”这个词,但这些似乎都不适合我。奇怪的是,在没有处理程序的情况下,如果处理程序中的所有代码都是在button1条件语句中编写的,那么。。。代码运行得很好。您应该声明不同的UI Viewsbutton1等,并且在调用setContentView后,分配应该在onCreate方法中发生,因为在您为活动设置ContentView之前,视图不可用,这就是为什么它们为空,无论何时尝试访问它们,都会看到崩溃 你需要更换这些
final ImageButton button1 = (ImageButton) findViewById(R.id.motbut_unpressed);
final ImageView topbar2 = (ImageView) findViewById(R.id.topbarmain);
final ImageButton button2 = (ImageButton) findViewById(R.id.posbut_unpressed);
final ImageButton button3 = (ImageButton) findViewById(R.id.envbut_unpressed);
final LinearLayout bcfield = (LinearLayout) findViewById(R.id.babcocklayout);
final ImageButton arrowup = (ImageButton) findViewById(R.id.uparrow);
final ImageButton arrowdown = (ImageButton) findViewById(R.id.downarrow);
final ImageView navbar = (ImageView) findViewById(R.id.infospace);
final TextView dynamictext = (TextView) findViewById(R.id.dynamictxt);
final TextView dynamicheader = (TextView) findViewById(R.id.dynamiched);
与
在setContentView之后的onCreate函数中,执行如下赋值
button1 = (ImageButton) findViewById(R.id.motbut_unpressed);
topbar2 = (ImageView) findViewById(R.id.topbarmain);
button2 = (ImageButton) findViewById(R.id.posbut_unpressed);
button3 = (ImageButton) findViewById(R.id.envbut_unpressed);
bcfield = (LinearLayout) findViewById(R.id.babcocklayout);
arrowup = (ImageButton) findViewById(R.id.uparrow);
arrowdown = (ImageButton) findViewById(R.id.downarrow);
navbar = (ImageView) findViewById(R.id.infospace);
dynamictext = (TextView) findViewById(R.id.dynamictxt);
dynamicheader = (TextView) findViewById(R.id.dynamiched);
可能重复尝试在setContentView之后将所有findViewById调用移动到onCreate方法中!你能解释一下为什么你认为在调用setContentViewR.layout.activity_main之前可以调用findViewById吗?@Tom你是问我还是问他?啊,我假设UIThread在某些情况下会预加载所有内容,特别是在设置内容视图之前预加载全局对象将整个块移动到该方法中?不做任何其他操作?您应该将Viewsbutton1等声明在它们所在的外部,因为您希望能够从类中访问它们,但视图button1=ImageButton findViewByIdR.id.motbut_的分配未经处理;应该在oncreate中,这样更好。那么你不认为你应该在你的答案中写下这句话吗?太棒了,非常感谢你,处理者没有引起问题吗?@adambroni关于处理者我不知道你为什么需要它。在点击按钮1的内部,为什么不能简单地更改不同视图的可见性和背景?
button1 = (ImageButton) findViewById(R.id.motbut_unpressed);
topbar2 = (ImageView) findViewById(R.id.topbarmain);
button2 = (ImageButton) findViewById(R.id.posbut_unpressed);
button3 = (ImageButton) findViewById(R.id.envbut_unpressed);
bcfield = (LinearLayout) findViewById(R.id.babcocklayout);
arrowup = (ImageButton) findViewById(R.id.uparrow);
arrowdown = (ImageButton) findViewById(R.id.downarrow);
navbar = (ImageView) findViewById(R.id.infospace);
dynamictext = (TextView) findViewById(R.id.dynamictxt);
dynamicheader = (TextView) findViewById(R.id.dynamiched);