在Fragment Android中调用自定义对话框

时间:2014-04-14 06:13:27

标签: android android-intent android-fragments

在导航抽屉中我调用Fragment类之后在Fragment Class中我想创建自定义对话框然后收到错误

 04-14 11:20:46.974: E/AndroidRuntime(5245): FATAL EXCEPTION: main
 04-14 11:20:46.974: E/AndroidRuntime(5245):
 java.lang.RuntimeException: Binary XML file line #17: You must supply
 a layout_height attribute. 04-14 11:20:46.974: E/AndroidRuntime(5245):
    at
 android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5457)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5592)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:554)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.view.LayoutInflater.inflate(LayoutInflater.java:477) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 com.dwellesque.MyProfile.onCreateView(MyProfile.java:54) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.app.BackStackRecord.run(BackStackRecord.java:635) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.os.Handler.handleCallback(Handler.java:615) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.os.Handler.dispatchMessage(Handler.java:92) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 android.os.Looper.loop(Looper.java:137) 04-14 11:20:46.974:
 E/AndroidRuntime(5245):    at
 android.app.ActivityThread.main(ActivityThread.java:4745) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 java.lang.reflect.Method.invokeNative(Native Method) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 java.lang.reflect.Method.invoke(Method.java:511) 04-14 11:20:46.974:
 E/AndroidRuntime(5245):    at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 04-14 11:20:46.974: E/AndroidRuntime(5245):    at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 04-14
 11:20:46.974: E/AndroidRuntime(5245):  at
 dalvik.system.NativeStart.main(Native Method)

我的java代码是我调用自定义对话框的地方

public class MyProfile extends Fragment {

Button btnSave,btnChange;
EditText txtFname;
EditText txtLane;
EditText txtEmil;
EditText txtCity;
EditText txtState;
EditText txtZip;
String strFname,strLname,strEmail,strCity,strState,strZip;
Document doc;
ArrayList<String> mylist= new ArrayList<String>();
SharedPreferences preferences = null;
String prefusername,prefemail,prefpass;
/*ArrayList<String> arFname= new ArrayList<String>();
ArrayList<String> arLname= new ArrayList<String>();
ArrayList<String> arEmail= new ArrayList<String>();
ArrayList<String> arCity= new ArrayList<String>();
ArrayList<String> arState= new ArrayList<String>();
ArrayList<String> arZip= new ArrayList<String>(); */
public MyProfile(){

}

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {

     View rootView = inflater.inflate(R.layout.myprofile, container, false);

     preferences=getActivity().getSharedPreferences("Login", getActivity().MODE_PRIVATE);
     prefusername=preferences.getString("USERNAME", "DEFAULT");
     prefemail=preferences.getString("EMAIL", "DEFAULT");
     prefpass= preferences.getString("PASSWORD", "DEFAULT");
     txtFname= (EditText) rootView.findViewById(R.id.edit_Profile_Fname);
     txtLane =(EditText) rootView.findViewById(R.id.edit_Profile_Lname);
     txtEmil= (EditText) rootView.findViewById(R.id.edit_Profile_email);
     txtCity= (EditText) rootView.findViewById(R.id.edit_Profile_City);
     txtState= (EditText) rootView.findViewById(R.id.State_spinner);
     txtZip = (EditText) rootView.findViewById(R.id.edit_Profile_Zip);
      btnChange = (Button) rootView.findViewById(R.id.edit_Profile_PASSWORD);

      strFname= getEditText(txtFname);
      strLname= getEditText(txtLane);
      strCity= getEditText(txtCity);
      strState=  getEditText(txtState);
      strEmail= getEditText(txtEmil);
      strZip= getEditText(txtZip);

      btnChange.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "CLICK", Toast.LENGTH_LONG).show();
                            changePass();
        }
    });



     return rootView;
 }



 void changePass()
 {
        final Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(R.layout.change_password);
        dialog.setTitle("Change password");
        final EditText txtPassword = (EditText) dialog.findViewById(R.id.changepwd);
        final EditText txtVpassword = (EditText) dialog.findViewById(R.id.changevpwd);
        Button btnOk = (Button) dialog.findViewById(R.id.btnch_ok);
        Button btnCancle = (Button) dialog.findViewById(R.id.btnch_cancle);


 }
 }

Dialog change_password xml代码在这里

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg" >

<LinearLayout
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:orientation="vertical" >



    <EditText
        android:id="@+id/changepwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:singleLine="true"
        android:password="true"
        android:hint="Password" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/changevpwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Verify Password"
        android:password="true"
        android:singleLine="true" />

    <LinearLayout 
   android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:layout_below="@+id/edit_Verify"
        android:orientation="horizontal" android:layout_centerInParent="true">


        <Button
            android:id="@+id/btnch_ok"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded"
            android:text="Ok" >
            </Button>


        <Button
            android:id="@+id/btnch_cancle"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_below="@+id/check_Pwd"
            android:layout_marginLeft="20dip"
            android:background="@drawable/rounded"
            android:text="Cancel" >
                </Button>
    </LinearLayout>

    </LinearLayout>

 </RelativeLayout>

myprofile布局就在这里

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_gravity="center" 
android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:id="@+id/myprofilelayout"
android:background="@drawable/bg">


<ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:scrollbars="none"
    android:layout_marginLeft="20dip" 
    android:layout_marginRight="20dip"
    android:layout_marginTop="10dip" 
    android:layout_below="@+id/rel_Profile">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
        >
            <!-- First Name -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:id="@+id/txt_profile_Fname"
                android:text="First Name"
                 android:textStyle="bold"
                android:textColor="#4a596e"
                android:textSize="14dip" 
                android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
               android:inputType="textCapWords"

                android:layout_height="wrap_content" 
                android:id="@+id/edit_Profile_Fname"
                 android:background="@android:drawable/editbox_background"
                android:singleLine="true" 
                android:layout_weight="0.25"></EditText>
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dip" >
            <!--Last Name -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/txt_profile_Lname"
                android:text="Last Name" android:textStyle="bold"
                android:textColor="#4a596e"
                android:textSize="14dip" android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
                android:inputType="textCapWords"
                 android:background="@android:drawable/editbox_background"
                android:layout_height="wrap_content" android:id="@+id/edit_Profile_Lname"
                android:layout_weight="0.25"></EditText>
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_marginTop="10dip"  >
            <!--Last Name -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/txt_profile_email"
                android:text="Email" android:textStyle="bold"
                android:textColor="#4a596e"
                android:textSize="14dip" android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
                android:textIsSelectable="false"
               android:inputType="textCapWords"
                 android:background="@android:drawable/editbox_background"
                android:layout_height="wrap_content" android:id="@+id/edit_Profile_email"
                android:singleLine="true"
                android:layout_weight="0.25"></EditText>
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_marginTop="10dip" >
            <!-- City -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/txt_profile_City"
                android:text="City" android:textStyle="bold" android:textSize="14dip"
                android:layout_below="@+id/keyword1"
                android:textColor="#4a596e"
                android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
                android:inputType="textCapWords"
                 android:background="@android:drawable/editbox_background"
                android:layout_height="wrap_content" android:id="@+id/edit_Profile_City"
                android:singleLine="true" android:layout_weight="0.25"></EditText>
        </LinearLayout>










        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_marginTop="10dip" >
            <!-- Zip -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/txt_profile_Zip1"
                android:textColor="#4a596e"
            android:text="State"  android:textStyle="bold" android:textSize="14dip"
                android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
                 android:background="@android:drawable/editbox_background"
                android:layout_height="wrap_content"  android:id="@+id/State_spinner"
               android:layout_weight="0.25"
                 android:singleLine="true"></EditText>
        </LinearLayout>





        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_marginTop="10dip"  >
            <!-- Zip -->
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/txt_profile_Zip"
                android:textColor="#4a596e"
                android:text="ZIP" android:textStyle="bold" android:textSize="14dip"
                android:layout_weight="0.5"></TextView>
            <EditText android:layout_width="fill_parent"
                 android:background="@android:drawable/editbox_background"
                android:layout_height="wrap_content" 
                android:id="@+id/edit_Profile_Zip"
                 android:layout_weight="0.25"
                 android:singleLine="true"></EditText>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:gravity="center" >

            <!-- Password -->

            <Button
            android:id="@+id/edit_Profile_PASSWORD"
            android:layout_width="200dip"
            android:layout_height="45dip"
           android:background="@drawable/rounded"
            android:textSize="18sp"
            android:text="Change Password" >
            </Button>
        </LinearLayout>



        <Button android:layout_width="200dip"
            android:layout_gravity="center" 
            android:layout_height="45dip"
            android:layout_marginTop="10dip" 
             android:background="@drawable/rounded"
             android:text="Save"
              android:textSize="18sp"
            android:id="@+id/profile_save"></Button>

    </LinearLayout>



</ScrollView>


</RelativeLayout>

我的activity_main xml文件在这里

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView

        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#200000"
        android:dividerHeight="0.5dp"
        android:background="#FFFFFF"/>
 </android.support.v4.widget.DrawerLayout>

请帮助我如何在本课程中调用自定义对话框

提前致谢。

3 个答案:

答案 0 :(得分:0)

根据您的错误java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute. 04-14 11:20:46.974: E/AndroidRuntime(5245): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491),它显示您的布局中有一个视图缺少layout_height的属性。

在视图中仅添加layout_height属性。

 android:layout_height="wrap_content"

答案 1 :(得分:0)

04-14 11:20:46.974: E/AndroidRuntime(5245):    at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5592) 
04-14 11:20:46.974: E/AndroidRuntime(5245):    at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)

我在activity_main.xml猜测你有一个FrameLayout ViewGroup android:layout_height属性正在消失。

答案 2 :(得分:0)

好的,您在change_password.xml布局中所做的是为孩子的wrap_contentRelativeLayout高度定义fill_parent高度LinearLayout } {id text。现在,如果您是布局解释器,您将如何解决此循环布局依赖性问题? ;)使LinearLayout的高度固定或wrap_content,一切都应按预期工作。