android - 在自定义 DialogFragment 中初始化 TextView

标签 android android-layout textview

我已经创建了一个自定义 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

请教我如何解决这个问题。谢谢!

最佳答案

您正在访问 nameBox 而未对其进行初始化。如果 TextView 属于 dialogfragment.xml,您必须使用 inflater 返回的 View 来初始化它:

View view = inflater.inflate(R.layout.dialogfragment, null);
nameBox = (TextView) view.findViewById(R.id.name_box);
nameBox.setText(myValue);
builder.setView(view);

关于android - 在自定义 DialogFragment 中初始化 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29050618/

相关文章:

android - Android View 中的 setMinHeight 和 setMinimumHeight 有什么区别?

android - 如何在组装字符串时为 TextView 生成样式文本?

android - TextView 失败

android - 将文本和图像彼此完美对齐

java - 在 multiautocompletetextview 中输入双空格来替换逗号

android - 我的所有 Toasts 文本都显示在中心之外

android - 如何在 android studio 中了解开发者代码责备

android - 从android上传图片到golang服务器并保存在mongodb中

android - 如何在编辑文本中像阴影一样放置内部褪色?

android - DialogFragment在Android中的位置