我创建了一个自定义的DialogFragment,我想初始化TextViews以将文本设置到它们中。我一直在收到错误。请帮助。
这是我的DialogFragment类。
package com.example.fproject;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.TextView;
public class DFragment extends DialogFragment {
TextView nameBox, departmentBox, officeBox, emailBox, phonenoBox, statusBox;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
Bundle mArgs = getArguments();
String myValue = mArgs.getString("firstname");
nameBox.setText(myValue);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialogfragment, null))
// Add action buttons
.setTitle("User Profile")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
});
return builder.create();
}
}
这是我的dialogfragment.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/user_pic"
android:layout_width="wrap_content"
android:layout_height="235dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_marginTop="20dp"
android:src="@drawable/icon_user" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:layout_marginTop="16dp"
android:text="Phone : " />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="18dp"
android:text="Office : " />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/user_pic"
android:layout_marginLeft="16dp"
android:layout_marginTop="14dp"
android:text="Full Name : " />
<TextView
android:id="@+id/user_department"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/user_name"
android:text="user_department" />
<TextView
android:id="@+id/user_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/user_email"
android:text="user_phone" />
<TextView
android:id="@+id/user_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView7"
android:layout_alignRight="@+id/user_phone"
android:text="user_status" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="19dp"
android:text="Department : " />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginBottom="17dp"
android:text="Status : " />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="20dp"
android:text="Email : " />
<TextView
android:id="@+id/user_office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_alignLeft="@+id/user_department"
android:text="user_office" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_centerHorizontal="true"
android:text="user_name" />
<TextView
android:id="@+id/user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_alignLeft="@+id/user_office"
android:text="user_email" />
这是我的logcat错误:
03-14 16:01:10.925: E/AndroidRuntime(485): FATAL EXCEPTION: main
03-14 16:01:10.925: E/AndroidRuntime(485): java.lang.NullPointerException
03-14 16:01:10.925: E/AndroidRuntime(485): at com.example.fproject.DFragment.onCreateDialog(DFragment.java:26)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:110)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.os.Handler.handleCallback(Handler.java:587)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.os.Looper.loop(Looper.java:132)
03-14 16:01:10.925: E/AndroidRuntime(485): at android.app.ActivityThread.main(ActivityThread.java:4025)
03-14 16:01:10.925: E/AndroidRuntime(485): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:01:10.925: E/AndroidRuntime(485): at java.lang.reflect.Method.invoke(Method.java:491)
03-14 16:01:10.925: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-14 16:01:10.925: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-14 16:01:10.925: E/AndroidRuntime(485): at dalvik.system.NativeStart.main(Native Method)
03-14 16:06:11.015: I/Process(485): Sending signal. PID: 485 SIG: 9
请告诉我如何解决这个问题。谢谢!
答案 0 :(得分:3)
您正在访问nameBox
而未初始化它。如果TextView
属于dialogfragment.xml,则必须使用inflater
返回的视图对其进行初始化:
View view = inflater.inflate(R.layout.dialogfragment, null);
nameBox = (TextView) view.findViewById(R.id.name_box);
nameBox.setText(myValue);
builder.setView(view);
答案 1 :(得分:0)
第一次崩溃是访问&#34; mArgs&#34;,这是空的。 您可以通过handle参数将数据传递给片段:
DFragment fragment = new DFragment();
Bundle args = new Bundle();
args.putString("firstname", "someName");
fragment.setArguments(args);
fragment.show(fm, "");
第二次崩溃是访问&#34; nameBox&#34;它没有连接到布局xml。
View view = inflater.inflate(R.layout.dialogfragment, null);
builder.setView(view)
// Add action buttons
.setTitle("User Profile")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
});
nameBox = (TextView) view.findViewById(R.id.textView6);
nameBox.setText(myValue);
答案 2 :(得分:0)
实际上this是使用DialogFragment的正确方法:
public class DFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialogfragment, container,
false);
getDialog().setTitle("DialogFragment Tutorial");
// Do something else
return rootView;
}
}