Android模拟器:抱歉 - 应用程序(app_name)意外停止。请再试一次

时间:2015-08-08 17:05:35

标签: android eclipse crash android-emulator

我是Android的初学者,我正在制作一个使用Eclipse + ADT使用Java代码使用RelativeLayout的Login UI应用程序。代码中没有错误,但是当我在模拟器上运行应用程序时,会出现对话框说 - 抱歉 - 应用程序(app_name)意外停止。请再试一次。这是我的MainActivity.java代码:

package dg.relativelayoutincode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
RelativeLayout main;
EditText userNameValue, passwordValue;
TextView message, userName, password;
Button login;
LayoutParams messageDimensions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
    createMessageTextView();
    main.addView(message, messageDimensions);
    setContentView(main);
}

private void init() {
    // TODO Auto-generated method stub
    main = new RelativeLayout(this);
    LayoutParams mainDimensions = new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    main.setLayoutParams(mainDimensions);
    userNameValue = new EditText(this);
    passwordValue = new EditText(this);
    message = new TextView(this);
    userName = new TextView(this);
    password = new TextView(this);
    login = new Button(this);

}

private void createMessageTextView() {
    // TODO Auto-generated method stub
    LayoutParams messageDimensions = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    messageDimensions.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    message.setText("please login first");
    message.setLayoutParams(messageDimensions);
}}

以下是我运行App时Logcat显示的内容:

08-08 22:24:57.498: D/AndroidRuntime(273): Shutting down VM
08-08 22:24:57.498: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-08 22:24:57.528: E/AndroidRuntime(273): FATAL EXCEPTION: main
08-08 22:24:57.528: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{dg.relativelayoutincode/dg.relativelayoutincode.MainActivity}: java.lang.NullPointerException
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-08 22:24:57.528: E/AndroidRuntime(273):  at java.lang.reflect.Method.invokeNative(Native Method)
08-08 22:24:57.528: E/AndroidRuntime(273):  at java.lang.reflect.Method.invoke(Method.java:521)
08-08 22:24:57.528: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-08 22:24:57.528: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-08 22:24:57.528: E/AndroidRuntime(273):  at dalvik.system.NativeStart.main(Native Method)
08-08 22:24:57.528: E/AndroidRuntime(273): Caused by: java.lang.NullPointerException
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:3573)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3723)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1123)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:938)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1975)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.view.ViewGroup.addView(ViewGroup.java:1845)
08-08 22:24:57.528: E/AndroidRuntime(273):  at dg.relativelayoutincode.MainActivity.onCreate(MainActivity.java:24)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-08 22:24:57.528: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-08 22:24:57.528: E/AndroidRuntime(273):  ... 11 more

请帮忙。

1 个答案:

答案 0 :(得分:0)

试 改变

main.addView(message, messageDimensions);

main.addView(message);