gpt4 book ai didi

android - 设置onClickListener时,应用程序崩溃了吗?

转载 作者:行者123 更新时间:2023-12-03 17:41:16 28 4
gpt4 key购买 nike

大家好,我正在努力解决一个奇怪的问题。
我将ViewFlipper更改为ViewPager,此后我的应用崩溃了。我发现崩溃的不是Viewpager,而是button.setOnClickListener方法。我注释掉了其他所有内容,因此我注意到,如果用该方法注释掉我的两个Button,则App可以正常工作。而且它不是onClick()内的东西,我将其清除了,即使它为空也崩溃了。但这是一个与我战斗的代码:

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);




MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
new Toolbox();

push = (Button)findViewById(R.id.pushbutton);
push.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
head=head_field.getText().toString();
note=note_field.getText().toString();
try {
Toolbox.createTask(userToken,task, Dashboard_id, head, note, service);
head_field.setText("");
note_field.setText("");
} catch (ProducteevException e) {
return;

}
}
});

这是按钮xml摘录:
<Button
android:id="@+id/pushbutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:background="@drawable/push_button" />

这里是LogCat
01-12 20:50:24.402: W/dalvikvm(28405): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-12 20:50:24.402: E/AndroidRuntime(28405): FATAL EXCEPTION: main
01-12 20:50:24.402: E/AndroidRuntime(28405): java.lang.RuntimeException: Unable to start activity ComponentInfo{producteev.push/producteev.push.Producteev_pushActivity}: java.lang.NullPointerException
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.os.Looper.loop(Looper.java:123)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-12 20:50:24.402: E/AndroidRuntime(28405): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 20:50:24.402: E/AndroidRuntime(28405): at java.lang.reflect.Method.invoke(Method.java:507)
01-12 20:50:24.402: E/AndroidRuntime(28405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-12 20:50:24.402: E/AndroidRuntime(28405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-12 20:50:24.402: E/AndroidRuntime(28405): at dalvik.system.NativeStart.main(Native Method)
01-12 20:50:24.402: E/AndroidRuntime(28405): Caused by: java.lang.NullPointerException
01-12 20:50:24.402: E/AndroidRuntime(28405): at producteev.push.Producteev_pushActivity.onCreate(Producteev_pushActivity.java:122)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-12 20:50:24.402: E/AndroidRuntime(28405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-12 20:50:24.402: E/AndroidRuntime(28405): ... 11 more

如果有人知道这个问题,那将是惊人的。

最佳答案

确保您的推送按钮ID是corect,看来您的findViewById()返回null ...

关于android - 设置onClickListener时,应用程序崩溃了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8841398/

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