嗨,我正在尝试通过在整个屏幕上设置 ontouchlistener 来开始一项新活动。但是,当我在 android 模拟器中单击屏幕时,我遇到了 ComponentInfo 的实例化问题,特别是 nullpointerexception。我的问题可能是我从第二堂课中删除了 saveinstantstate==null
public class LoadScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_screen);
View view=(FrameLayout) findViewById(R.id.container);
view.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent(v.getContext(),SignUp.class));
return true;
}
});
}
}
public class SignUp extends Activity {
public Button signup=(Button) findViewById(R.id.CompleteRegistration);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
signup.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),MainActivity.class));
}
});
}
}
07-23 01:42:26.200: E/AndroidRuntime(1138): FATAL EXCEPTION: main
07-23 01:42:26.200: E/AndroidRuntime(1138): Process: com.example.demoww, PID: 1138
07-23 01:42:26.200: E/AndroidRuntime(1138): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.demoww/com.example.demoww.SignUp}: java.lang.NullPointerException
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.os.Looper.loop(Looper.java:136)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-23 01:42:26.200: E/AndroidRuntime(1138): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 01:42:26.200: E/AndroidRuntime(1138): at java.lang.reflect.Method.invoke(Method.java:515)
07-23 01:42:26.200: E/AndroidRuntime(1138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-23 01:42:26.200: E/AndroidRuntime(1138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-23 01:42:26.200: E/AndroidRuntime(1138): at dalvik.system.NativeStart.main(Native Method)
07-23 01:42:26.200: E/AndroidRuntime(1138): Caused by: java.lang.NullPointerException
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.Activity.findViewById(Activity.java:1884)
07-23 01:42:26.200: E/AndroidRuntime(1138): at com.example.demoww.SignUp.<init>(SignUp.java:15)
07-23 01:42:26.200: E/AndroidRuntime(1138): at java.lang.Class.newInstanceImpl(Native Method)
07-23 01:42:26.200: E/AndroidRuntime(1138): at java.lang.Class.newInstance(Class.java:1208)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-23 01:42:26.200: E/AndroidRuntime(1138): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-23 01:42:26.200: E/AndroidRuntime(1138): ... 11 more