-1

嗨,我正在尝试通过在整个屏幕上设置 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
4

4 回答 4

1

您应该移动此代码

Button signup=(Button) findViewById(R.id.CompleteRegistration);

在您的注册活动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));

    }

  });
  }

 }
于 2014-07-23T05:51:43.487 回答
0

移动

Button signup=(Button) findViewById(R.id.CompleteRegistration);

下线setContentView()

于 2014-07-23T06:10:50.940 回答
0

除了将查找按钮命令移动到 onCreate(...) 方法的建议之外,我认为问题可能是 R.layout.activity_sign_up 布局中 CompleteRegistration Button 的错误定义以及 R.layout.activity_load_screen 布局中的内容 FrameLayout !

请检查这些事情。如果这些是正确的,您应该发送这部分代码以找出问题。

于 2014-07-23T06:21:23.853 回答
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 类调用中的覆盖方法从 onCreate() 处理的 XML 中查找由 id 属性标识的视图。否则返回 null,因此您正在经历 NPE

public View findViewById(int id) {
        return getWindow().findViewById(id);
}
于 2014-07-23T06:27:47.290 回答