使用Butterknife从DialogFragment绑定EditText

时间:2016-09-24 18:42:47

标签: java android butterknife

我正在尝试从对话框Edittexts中的2 Fragment获取字符串,但是当activity启动时,我的应用仍会崩溃。能够从对话框中绑定edittexts是一种问题。我不确定绑定它们的正确方法。

这是崩溃:

09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
 at butterknife.ButterKnife.bind(ButterKnife.java:322)
 at butterknife.ButterKnife.bind(ButterKnife.java:237)
 at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
 at android.app.Activity.performCreate(Activity.java:6237)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '@Nullable' annotation.
 at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
 at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
 at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
 at butterknife.ButterKnife.bind(ButterKnife.java:319)
 at butterknife.ButterKnife.bind(ButterKnife.java:237) 
 at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37) 
 at android.app.Activity.performCreate(Activity.java:6237) 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是活动:

import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;

public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {

@Bind(R.id.summaryTextView) TextView mSummaryTextView;
@Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
@Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
@Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;

private Firebase mFirebaseRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);
    ButterKnife.bind(this);
    mDeleteAccountButton.setOnClickListener(this);


    //GET REFERENCE TO FIREBASE
    mFirebaseRef = new Firebase(Constants.FIREBASE_URL);

    //SETS FONT FOR TITLE
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
    mSummaryTextView.setTypeface(tf);

}

@Override
public void onClick(View view) {
    if(view == mDeleteAccountButton) {
        createAlertDialog();
    }
}

public void deleteUser(String userEmail, String userPassword) {
    Firebase.ResultHandler handler = new Firebase.ResultHandler() {
        @Override
        public void onSuccess() {
            Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FirebaseError firebaseError) {
            Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
        }

    };
    mFirebaseRef.removeUser(userEmail, userPassword, handler );
}

public void createAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
    LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
    View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
    builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));

    builder.setMessage(R.string.dialog_message)
            .setTitle(R.string.dialog_title);
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });
    builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
                String userEmail = mUserEmailEditText.getText().toString();
                String userPassword = mUserPasswordEditText.getText().toString();
                deleteUser(userEmail, userPassword);
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
}
}

这是活动的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="@+id/relativeLayout">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_centerVertical="true"
    android:scaleType="fitXY"
    android:layout_centerHorizontal="true"
    android:src="@drawable/background1" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Account Details"
    android:textColor="#0288D1"
    android:id="@+id/summaryTextView"
    android:layout_alignParentTop="true"
    android:textSize="30sp"
    android:layout_centerInParent="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Photo"
    android:textColor="#0288D1"
    android:background="@drawable/buttonshape"
    android:id="@+id/addPhotoButton"
    android:layout_below="@+id/summaryTextView"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:layout_marginTop="25dp" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/userPhotoImageView"
    android:layout_marginTop="25dp"
    android:layout_below="@+id/addPhotoButton"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/current_user_s_image"
    android:src="@drawable/blank_user" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Email"
    android:textColor="#0288D1"
    android:id="@+id/changeEmailButton"
    android:background="@drawable/buttonshape"
    android:layout_below="@+id/userPhotoImageView"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:layout_marginTop="25dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Password"
    android:textSize="25sp"
    android:textColor="#0288D1"
    android:layout_marginTop="25dp"
    android:background="@drawable/buttonshape"
    android:id="@+id/changePasswordEmailButton"
    android:layout_below="@+id/changeEmailButton"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete Account"
    android:textSize="25sp"
    android:textColor="#0288D1"
    android:layout_marginTop="25dp"
    android:id="@+id/deleteAccountButton"
    android:layout_below="@+id/changePasswordEmailButton"
    android:layout_centerHorizontal="true"
    android:background="@drawable/buttonshape" />

以下是AlertDialog的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/userEmailEditText"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="Enter Email" />
<EditText
    android:id="@+id/userPasswordEditText"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="Enter Password"/>
</LinearLayout>

显示如何正确绑定mUserEmailEditTextmUserPasswordEditText

的任何示例

谢谢

2 个答案:

答案 0 :(得分:1)

当您使用bind时,应该是来自活动布局中的现有ID,因为您在xml中没有它会崩溃。不要绑定EditText并只在AlertDialog中创建它们,并且即时使用setParams改变他们的布局

答案 1 :(得分:0)

@Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
@Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;

您无法绑定viewxml来自Activity's view的{​​{1}}的{​​{1}}。所以从上面的行中删除@Bind,如下所示。

EditText mUserEmailEditText;
EditText mUserPasswordEditText;
inflate方法中创建dialog时,

createAlertDialog()。所以替换以下2行

View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));

View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);