2017-02-21 117 views
0

我是新的在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"); 

} 
+0

你在哪里夸大了你的观点? –

+0

请发布您的onCreate方法,因为崩溃发生在那里 –

+0

从你调用“addURL”方法? –

回答

0

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

我通常建议只使用ButterKnife作为类的主视图并调用findViewById来查找对话框的视图等。

1

您可以使用私有类来绑定片段中对话框布局的视图,就像这样。

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

它应该做的伎俩。