【问题标题】:IllegalStateException on Alertdialog and ButterKnifeAlertdialog 和 ButterKnife 上的 IllegalStateException
【发布时间】:2023-04-09 08:29:02
【问题描述】:

我是 android 上的新手,我想在我的代码上使用黄油刀。在我的实践中,我在主要活动布局中有一个 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" />

这是我的点击:

   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”方法?
  • 我认为 EditText(editText_add_url) 不是您在 addURL 方法中传递的视图的子级。
  • @DamianJäger 我已经编辑了我的帖子。

标签: android android-alertdialog butterknife


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    //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;
    

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

    View inflate = View.inflate(getContext(), R.layout.dialog_add_url, null);
    dialogViewHolder = new DialogViewHolder(inflate);
    

    它应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2014-12-20
      相关资源
      最近更新 更多