如何将setText从其他类中的自定义对话框中删除?

时间:2015-06-03 20:42:28

标签: java android nullpointerexception dialog

我创建了一个对话框类,我想在其他几个类中使用它们,并根据它们应该显示不同信息的类。使用工作按钮和xml中已设置的文本显示对话框本身没有问题。但是当我尝试使用setText设置我自己的文本时,应用程序崩溃并给我java.lang.NullPointerException。可能是什么问题?

这是我的自定义对话框类

ckan.plugins = stats text_view recline_view pages

这是我尝试称之为的主要内​​容;

public class CustomDialogInfoClass extends Dialog implements     View.OnClickListener {
Button ok;
TextView myTextView;
Typeface myFont;


public CustomDialogInfoClass(Context context) {
    super(context);
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog_not_demo);
        CustomDialogInfoClass c = new CustomDialogInfoClass(getContext());

        ok = (Button)findViewById(R.id.btnOk);
        myTextView = (TextView) c.findViewById(R.id.textViewDialog);
        ok.setOnClickListener(this);

        myFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/font.ttf");
        ok.setTypeface(myFont);
        myTextView.setTypeface(myFont);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnOk:
                dismiss();
                break;
            default:
        }
        dismiss();
    }
}

XML

public class MainActivity extends ActionBarActivity {

TextView myTextview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CustomDialogInfoClass dialogInfo = new CustomDialogInfoClass(this);
    myTextview = (TextView) findViewById(R.id.textViewDialog);

    myTextview.setText("Lorem ipsum dolor sit amet"); <----Null.pointer error 
    dialogInfo.show();
}

和logcat如果有帮助吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:orientation="vertical"
    android:background="@drawable/diabox"
    android:weightSum="1">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textViewDialog"
    android:layout_margin="15dp"
    android:textColor="@android:color/white"
    android:text="Test text" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ok"
    android:id="@+id/btnOk"
    android:layout_gravity="center_horizontal" />

2 个答案:

答案 0 :(得分:3)

您不应该从活动

访问文本视图

在你的对话框中你有这个构造函数

public CustomDialogInfoClass(Context context) { super(context); }

所以也要制作这个:

public CustomDialogInfoClass(Context context,String text) {
    CustomDialogInfoClass(context);
    this.text = text;
}

并在对话框类中创建一个String字段

String text;

并在对话框中设置TextView

myTextView = (TextView) c.findViewById(R.id.textViewDialog);
myTextView.setText(text);

并在您的活动中传递您的文字:

CustomDialogInfoClass c = new CustomDialogInfoClass(getContext(), "MY TEXT");

答案 1 :(得分:1)

您需要在onCreate方法期间从对话框中设置TextView的值。

要将参数传递给对话框,正确的方法是在Dialog类中使用静态方法返回Object的新实例。 此方法可以接受参数并将它们放入Bundle中。现在,您可以将此捆绑包设置为创建的对话框

的参数
public static YourDialog getInstance(String par1, int par2) {

    YourDialog dialog = new YourDialog()

    Bundle bundle = new Bundle();
    bundle.putString(TAG, par1);
    bundle.putInt(TAG2, par2);
    dialog.setArguments(bundle);

    return dialog;
} 

此时在对话框的OnCreate方法中,您可以使用之前提供的标记使用getArguments()获取Bundle并设置TextView

完成MainActivity后,获取对话框的实例并调用show()来显示它

希望有所帮助