所以,如果知道这是一个新手就不明显了 我正在与Intent合作开设一项新活动(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();
}
}
它像魔术一样工作,活动开放没有任何问题。
但是当我向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)
答案 0 :(得分:1)
Butter Knife并没有神奇地束缚这些观点。你必须添加
android:layout_alignWithParentIfMissing
到Task taskA = Task.Factory.StartNew(() => RunGet());
taskA.Wait(1000);
也是如此。像这样:
ButterKnife.bind(this);
答案 1 :(得分:0)
@tynn
所以不要做
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);
}
}
我做了
package com.mhs.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import butterknife.ButterKnife;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LabelledSpinner diabetess_type = (LabelledSpinner) findViewById(R.id.diabetes_type_spinner);
diabetess_type.setItemsArray(R.array.diabetes_type_list);
diabetess_type.setColor(R.color.white);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.diabetes_layout);
linearLayout.setVisibility(View.GONE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
ButterKnife.bind(this);
}
}
}
令人惊讶的是,它有效