gpt4 book ai didi

android - 点击 Intent 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 16:48:42 25 4
gpt4 key购买 nike

所以,如果不是很明显,知道是一个新手
我正在与Intent合作打开一个新 Activity (Main2Activity)
只要(Main2Activity)中什么都没有发生,一切都会很好。

我在Main2Activity中执行任何操作时会崩溃,
例如:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.checkbox_diabetic)
CheckBox diabetes;
@BindView(R.id.firs_next_button)
Button nextbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
@OnClick(R.id.firs_next_button)
public void setNextbutton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}

和:
package com.mhs.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import butterknife.BindView;
import butterknife.OnClick;

public class Main2Activity extends AppCompatActivity {

@BindView(R.id.diabetes_type_spinner)
LabelledSpinner diabetestypes;
@BindView(R.id.checkbox_diabetic)
CheckBox diabetes;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
}
}

它像魔术一样工作, Activity 打开没有任何问题。

但是,一旦我向Main2activity添加一个东西,它就会在单击下一步按钮时崩溃。例如:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import butterknife.BindView;
import butterknife.OnClick;

public class Main2Activity extends AppCompatActivity {
@BindView(R.id.diabetes_type_spinner)
LabelledSpinner diabetestypes;
@BindView(R.id.checkbox_diabetic)
CheckBox diabetes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();

diabetestypes.setItemsArray(R.array.diabetes_type_list);
}
}

崩溃报告:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mhs.myapplication, PID: 27891
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhs.myapplication/com.mhs.myapplication.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mhs.myapplication.LabelledSpinner.setItemsArray(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mhs.myapplication.LabelledSpinner.setItemsArray(int)' on a null object reference
at com.mhs.myapplication.Main2Activity.onCreate(Main2Activity.java:22)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

最佳答案

黄油刀不会神奇地束缚视线。您必须添加

ButterKnife.bind(this);

以及 Main2Activity。像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this); // Need to add this!
Intent intent = getIntent();
}

关于android - 点击 Intent 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40938258/

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