我想创建用于循环对话框的对话框,并将edittext视图到对话框中。
我的错误日志:
1-29 14:54:23.314: E/AndroidRuntime(1099): FATAL EXCEPTION: main
01-29 14:54:23.314: E/AndroidRuntime(1099): java.lang.NullPointerException
01-29 14:54:23.314: E/AndroidRuntime(1099): at com.example.dialog_looping.MainActivity$dialog$1.onClick(MainActivity.java:44)
01-29 14:54:23.314: E/AndroidRuntime(1099): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
01-29 14:54:23.314: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 14:54:23.314: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:123)
01-29 14:54:23.314: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-29 14:54:23.314: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 14:54:23.314: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:507)
01-29 14:54:23.314: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-29 14:54:23.314: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-29 14:54:23.314: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)
我的完整应用代码:
package com.example.dialog_looping;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(new dialog(this));
}
public class dialog extends View {
public dialog(Context context) {
super(context);
//this.setNama(name.getText().toString());
LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Input Your Name")
.setView(inflater.inflate(R.layout.dialoginputname, null))
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText name;
name = (EditText)findViewById(R.id.name);
//String a = name.setText(name.getText().toString());
//int z = Integer.valueOf(a);
new AlertDialog.Builder(MainActivity.this)
.setTitle("YOur Name Is")
.setMessage(name.getText().toString())
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.show();
}
protected CharSequence nama() {
// TODO Auto-generated method stub
return null;
}
};
}
答案 0 :(得分:2)
更改
.setMessage(name())
要
.setMessage(name.getText().toString())
答案 1 :(得分:1)
为您改进代码:
EditText name;
name = (EditText)findViewById(R.id.name);
String a = name.getText().toString();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("something");
alertDialog.setMessage(name);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
立即尝试。 如果我的答案有帮助,请接受它。