如何将其他 class 中的自定义对话框设置为自定义对话框?
How setText to custom dialog from one class in others?
我创建了一个对话框 class,我想在其他几个 class 中使用它,并且根据 class 它们应该显示不同的信息。使用工作按钮和来自 xml 的已设置文本显示对话框本身没有问题。但是当我尝试使用 setText 设置我自己的文本时,应用程序崩溃并给我 java.lang.NullPointerException。可能是什么问题?
这是我的自定义对话框class
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();
}
}
这里是我尝试调用它的地方;
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();
}
xml
<?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" />
和 logcat 是否有帮助?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.name.appname/com.example.name.appname.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5064)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.name.appnamne.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6084)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5064)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at dalvik.system.NativeStart.main(Native Method)
您不应该从 activity
访问您的文本视图
在你的对话框中你有这个构造函数
public CustomDialogInfoClass(Context context) {
super(context);
}
所以也做这个:
public CustomDialogInfoClass(Context context,String text) {
CustomDialogInfoClass(context);
this.text = text;
}
并在对话框中创建一个字符串字段 class
String text;
并在对话框中设置您的 TextView
myTextView = (TextView) c.findViewById(R.id.textViewDialog);
myTextView.setText(text);
并在您的 activity 中将您的文本传递到此处:
CustomDialogInfoClass c = new CustomDialogInfoClass(getContext(), "MY TEXT");
您需要在 onCreate 方法期间从对话框内部设置 TextView 的值。
要将参数传递给对话框,正确的方法是在 Dialog class 中使用静态方法 return Object 的新实例。
此方法可以接受参数并将它们放入 Bundle 中。现在您可以将此包设置为创建的 Dialog
的参数
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() 来显示它
希望对您有所帮助
我创建了一个对话框 class,我想在其他几个 class 中使用它,并且根据 class 它们应该显示不同的信息。使用工作按钮和来自 xml 的已设置文本显示对话框本身没有问题。但是当我尝试使用 setText 设置我自己的文本时,应用程序崩溃并给我 java.lang.NullPointerException。可能是什么问题?
这是我的自定义对话框class
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();
}
}
这里是我尝试调用它的地方;
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();
}
xml
<?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" />
和 logcat 是否有帮助?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.name.appname/com.example.name.appname.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5064)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.name.appnamne.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6084)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5064)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
at dalvik.system.NativeStart.main(Native Method)
您不应该从 activity
访问您的文本视图在你的对话框中你有这个构造函数
public CustomDialogInfoClass(Context context) {
super(context);
}
所以也做这个:
public CustomDialogInfoClass(Context context,String text) {
CustomDialogInfoClass(context);
this.text = text;
}
并在对话框中创建一个字符串字段 class
String text;
并在对话框中设置您的 TextView
myTextView = (TextView) c.findViewById(R.id.textViewDialog);
myTextView.setText(text);
并在您的 activity 中将您的文本传递到此处:
CustomDialogInfoClass c = new CustomDialogInfoClass(getContext(), "MY TEXT");
您需要在 onCreate 方法期间从对话框内部设置 TextView 的值。
要将参数传递给对话框,正确的方法是在 Dialog class 中使用静态方法 return Object 的新实例。 此方法可以接受参数并将它们放入 Bundle 中。现在您可以将此包设置为创建的 Dialog
的参数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() 来显示它
希望对您有所帮助