您好我正试图通过在整个屏幕上设置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
答案 0 :(得分:1)
您应该移动此代码
Button signup=(Button) findViewById(R.id.CompleteRegistration);
在 SignUp活动 中onCreate(..)
之后的setContentView(R.layout.activity_sign_up);
下
public class SignUp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
Button signup=(Button) findViewById(R.id.CompleteRegistration);
signup.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),MainActivity.class));
}
});
}
}
答案 1 :(得分:0)
移动
Button signup=(Button) findViewById(R.id.CompleteRegistration);
低于setContentView()
行。
答案 2 :(得分:0)
除了将on find按钮命令移入onCreate(...)方法的建议之外,我想 问题可能是R.layout.activity_sign_up布局中的CompleteRegistration按钮的错误定义以及R.layout.activity_load_screen布局中的内容FrameLayout!
请检查这些事情。 如果这些是正确的,你应该发送这部分代码来找出问题。
答案 3 :(得分:0)
移动此行
Button signup=(Button) findViewById(R.id.CompleteRegistration);
到
onCreate(...){
....
setContentView(layoutId);
Button signup=(Button) findViewById(R.id.CompleteRegistration);
}
因为当注册是对象创建时,则下一步是其字段 在指定
时,会初始化为默认值
Button signup=(Button)findViewById(R.id.CompleteRegistration);
Activity类调用中的overriden方法查找当前的视图 由处理的XML中的id属性标识 onCreate()。否则返回null,因此您遇到了NPE
public View findViewById(int id) {
return getWindow().findViewById(id);
}