Alertdialog和ButterKnife上的IllegalStateException

时间:2017-02-21 12:30:56

标签: android alertdialog butterknife

我是新的在Android上,我想在我的代码上使用butterknife。在我的练习中,我在主要活动布局中有一个FAB按钮:

   <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。对话框自定义布局中有一个Textedit视图:

    <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减速但我不知道如何在自定义布局的警报上使用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");

}

2 个答案:

答案 0 :(得分:0)

在onCreate()中调用ButterKnife.bind()时,ButterKnife会尝试从当前布局绑定所有@BindView带注释的视图。但是,R.layout.activity_main不包含id为R.id.editText_add_url的视图。因此它崩溃了。

我通常建议仅将ButterKnife用于类的主视图,并调用findViewById来查找对话框的视图等。

答案 1 :(得分:0)

您可以使用私有类绑定片段内对话框布局的视图,如下所示。

@-ms-viewport{
  width: device-width;
}

然后在addUrl函数中你可以使用它,

//To bind with the dialog view using butterKnife
public class DialogViewHolder {

    @BindView(R.id.editText_add_url)
    EditText editTextAddURL;

    DialogViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}
public DialogViewHolder dialogViewHolder;

它应该可以解决问题。