每当我按下我输入的“提交”按钮时,我的应用程序崩溃
问题描述:
每当我尝试选择“提交”我的输入对话框时,我的应用程序崩溃。任何人都可以帮助我,告诉我为什么它一直崩溃?我究竟做错了什么?非常感谢你的帮助。每当我按下我输入的“提交”按钮时,我的应用程序崩溃
MainActivity.java
public class MainActivity extends Activity {
private Button btnStartDomoos;
private Button btnChangeUrl;
private EditText edi ;
private EditText result;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
showToast();
btnStartDomoos = (Button) findViewById(R.id.buttonUrl);
btnChangeUrl = (Button) findViewById(R.id.btnChangeUrl);
edi = (EditText) findViewById(R.id.editText) ;
result = (EditText) findViewById(R.id.editTextDialogUserInput);
btnStartDomoos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("url", edi.getText().toString());
startActivity(intent);
}
});
btnChangeUrl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
private void showToast(){
Context context = getApplicationContext();
CharSequence text = "Bienvenue dans Domoos ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
的 'logcat的' 标签的内容:
02-27 19:50:23.023 5506-5506/? D/dalvikvm: Late-enabling CheckJNI
02-27 19:50:23.578 5506-5506/laurent.ch.domoos E/MoreInfoHPW_ViewGroup: Parent view is not a TextView
02-27 19:50:23.632 5506-5506/laurent.ch.domoos D/dalvikvm: GC_FOR_ALLOC freed 79K, 15% free 5388K/6268K, paused 18ms, total 18ms
02-27 19:50:23.640 5506-5506/laurent.ch.domoos I/dalvikvm-heap: Grow heap (frag case) to 8.101MB for 2160016-byte allocation
02-27 19:50:23.664 5506-5515/laurent.ch.domoos D/dalvikvm: GC_FOR_ALLOC freed <1K, 11% free 7497K/8380K, paused 24ms, total 24ms
02-27 19:50:23.757 5506-5510/laurent.ch.domoos D/dalvikvm: GC_CONCURRENT freed 3K, 11% free 7499K/8380K, paused 2ms+6ms, total 95ms
02-27 19:50:24.148 5506-5506/laurent.ch.domoos D/libEGL: loaded /system/lib/egl/libEGL_MRVL.so
02-27 19:50:24.156 5506-5506/laurent.ch.domoos D/libEGL: loaded /system/lib/egl/libGLESv1_CM_MRVL.so
02-27 19:50:24.445 5506-5506/laurent.ch.domoos D/libEGL: loaded /system/lib/egl/libGLESv2_MRVL.so
02-27 19:50:24.460 5506-5506/laurent.ch.domoos D/GC: <tid=5506> OES20 ===> GC Version : GC Ver SS_rls_pxa988_JB42_R1_RC2_GC13.15
02-27 19:50:24.484 5506-5506/laurent.ch.domoos D/OpenGLRenderer: Enabling debug mode 0
02-27 19:50:24.507 5506-5506/laurent.ch.domoos D/WritingBuddyImpl: getCurrentWritingBuddyView()
02-27 19:50:26.953 5506-5510/laurent.ch.domoos D/dalvikvm: GC_CONCURRENT freed 38K, 10% free 8097K/8944K, paused 3ms+1ms, total 35ms
02-27 19:50:27.070 5506-5506/laurent.ch.domoos D/WritingBuddyImpl: getCurrentWritingBuddyView()
02-27 19:50:29.906 5506-5506/laurent.ch.domoos D/AndroidRuntime: Shutting down VM
02-27 19:50:29.906 5506-5506/laurent.ch.domoos W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4167d930)
02-27 19:50:29.906 5506-5506/laurent.ch.domoos E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at laurent.ch.domoos.MainActivity$2$2.onClick(MainActivity.java:75)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
02-27 19:50:31.531 5506-5506/? I/Process: Sending signal. PID: 5506 SIG: 9
答
您正在使用错误的ID在
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput); // use id from xml "prompts"
或
result = (EditText) findViewById(R.id.editTextDialogUserInput); // use id from xml "main"
editTextDialogUserInput
应该是其中任何一个的其他东西。检查你的xml。
+0
非常感谢。它解决了我的问题。祝你有美好的一天。 – Laurent
可否请您发布logcat .. – Lal
我们需要看看logcat .. –
谢谢您的回复。 我希望这是你正在寻找的。 – Laurent