当我尝试通过列表视图项目启动新 activity 时,应用程序崩溃;由按钮实例化引起...?
App crashing when I try to start new activity through listview item; caused by button instantiation...?
尝试构建我的第一个应用程序,但遇到了一些困难;现在只是想测试我的应用程序,但点击列表视图项目会使它崩溃。
这是日志:
10-12 22:09:17.868 28134-28134/com.example.naomikoo.classalert E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.naomikoo.classalert/com.example.naomikoo.classalert.pickSection}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1839)
at com.example.naomikoo.classalert.pickSection.<init>(pickSection.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
pickSection.java 目前看起来是这样的:
public class pickSection extends AppCompatActivity {
String course;
String number;
String section;
Button button = (Button) findViewById(R.id.favouriteButton);
TextView courseInfo = (TextView) findViewById(R.id.courseinfo);
TextView capacity = (TextView) findViewById(R.id.capacity);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent2 = getIntent();
course = intent2.getStringExtra("com.naomi.classAlert.classList.course");
number = intent2.getStringExtra("com.naomi.classAlert.classList.number");
section = intent2.getStringExtra("com.naomi.classAlert.classList.section");
courseInfo.setText(course+number+" "+section);
capacity.setText("dummy text");
setContentView(R.layout.activity_pick_section);
}
public void onClick(View v) {
}
}
第 25 行是
按钮 button = (Button) findViewById(R.id.favouriteButton);
但不确定为什么这是错误的?如果有人能指出我正确的方向...
谢谢!
在 setContentView 之后调用 findViewById
setContentView(R.layout.activity_pick_section);
Button button = (Button) findViewById(R.id.favouriteButton);
TextView courseInfo = (TextView) findViewById(R.id.courseinfo);
TextView capacity = (TextView) findViewById(R.id.capacity);
尝试构建我的第一个应用程序,但遇到了一些困难;现在只是想测试我的应用程序,但点击列表视图项目会使它崩溃。
这是日志:
10-12 22:09:17.868 28134-28134/com.example.naomikoo.classalert E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.naomikoo.classalert/com.example.naomikoo.classalert.pickSection}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1839)
at com.example.naomikoo.classalert.pickSection.<init>(pickSection.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
pickSection.java 目前看起来是这样的:
public class pickSection extends AppCompatActivity {
String course;
String number;
String section;
Button button = (Button) findViewById(R.id.favouriteButton);
TextView courseInfo = (TextView) findViewById(R.id.courseinfo);
TextView capacity = (TextView) findViewById(R.id.capacity);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent2 = getIntent();
course = intent2.getStringExtra("com.naomi.classAlert.classList.course");
number = intent2.getStringExtra("com.naomi.classAlert.classList.number");
section = intent2.getStringExtra("com.naomi.classAlert.classList.section");
courseInfo.setText(course+number+" "+section);
capacity.setText("dummy text");
setContentView(R.layout.activity_pick_section);
}
public void onClick(View v) {
}
}
第 25 行是 按钮 button = (Button) findViewById(R.id.favouriteButton);
但不确定为什么这是错误的?如果有人能指出我正确的方向...
谢谢!
在 setContentView 之后调用 findViewById
setContentView(R.layout.activity_pick_section);
Button button = (Button) findViewById(R.id.favouriteButton);
TextView courseInfo = (TextView) findViewById(R.id.courseinfo);
TextView capacity = (TextView) findViewById(R.id.capacity);