gpt4 book ai didi

android - 当我尝试通过 ListView 项目开始新 Activity 时应用程序崩溃;由按钮实例化引起...?

转载 作者:行者123 更新时间:2023-11-30 01:50:44 27 4
gpt4 key购买 nike

尝试构建我的第一个应用程序,但遇到了一些困难;现在只是想测试我的应用程序,但点击 ListView 项会使它崩溃。

这是日志:

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.access$600(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.access$600(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);

关于android - 当我尝试通过 ListView 项目开始新 Activity 时应用程序崩溃;由按钮实例化引起...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com