我是新的在android上,我想在我的代碼上使用butterknife。在我的實踐,我有在主活動佈局的FAB按鈕:Alertdialog和ButterKnife上的IllegalStateException
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="10dp"
android:onClick="addURL"
android:src="@android:drawable/ic_input_add"
app:fabSize="normal" />
點擊我的FAB之後,alertDialog的出現。有在對話框的自定義佈局的文本編輯觀點:
<EditText
android:id="@+id/editText_add_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
這是我的onclick:
public void addURL(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add your URL:");
ButterKnife.setDebug(true);
ButterKnife.bind(this, view);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addURLToList();
}
});
和:
private void addURLToList() {
editTextAddURL.setText("Hi");
}
我得到editText_add_url參考像場:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.editText_add_url)
EditText editTextAddURL;
運行我得到這個錯誤後:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manifest.simplefiledownloadmanager/com.example.manifest.simplefiledownloadmanager.MainActivity}: java.lang.IllegalStateException: Required view 'editText_add_url' with ID 2131558531 for field 'editTextAddURL' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
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.IllegalStateException: Required view 'editText_add_url' with ID 2131558531 for field 'editTextAddURL' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
at butterknife.internal.Utils.findRequiredView(Utils.java:92)
at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:104)
at com.example.manifest.simplefiledownloadmanager.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:27)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at butterknife.ButterKnife.createBinding(ButterKnife.java:199)
at butterknife.ButterKnife.bind(ButterKnife.java:124)
at com.example.manifest.simplefiledownloadmanager.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
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)
我知道我的錯誤是因爲butterKnife減速,但我不知道怎麼用使用自定義佈局的alertdialog上的Butterknife。 謝謝你們。
編輯------------ 這是我的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
private void initView() {
//set Toolbar to Activity
setSupportActionBar(toolbar);
//set Fragment
getSupportFragmentManager().beginTransaction().add(R.id.frameLayout_container,
new ListDownloadFragment()).commit();
}
public void addURL(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add your URL:");
ButterKnife.setDebug(true);
ButterKnife.bind(MainActivity.this, view);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addURLToList();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
View inflate = getLayoutInflater().inflate(R.layout.dialog_add_url, null);
builder.setView(inflate);
dialog = builder.show();
}
private void addURLToList() {
editTextAddURL.setText("Hi");
}
你在哪裏誇大了你的觀點? –
請發佈您的onCreate方法,因爲崩潰發生在那裏 –
從你調用「addURL」方法? –