无法启动我的Android应用程序。 “关闭VM”错误

时间:2015-03-15 06:47:37

标签: android

我目前正在使用Eclipse来创建一个简单的界面,允许用户插入他们的名字,然后转向其他页面。

然而,我现在因为一些奇怪的问题而陷入困境。虽然它显示“关闭虚拟机”,但我怀疑这是主要原因。

这是我的代码

    package com.ytl.test;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import com.ytl.test.MainActivity;

public class MainActivity extends Activity {


    private TextView tvName;
    private EditText Name;
    private Button btnConfirm = new Button(this) ;
    private String stringName = new String();
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        tvName = new TextView(this);
        tvName.setText("Please enter your name: ");
        Name = new EditText(this);
        Name.setHint("Your Name");
        btnConfirm = new Button(this);
        btnConfirm.setText("Confirm");

        btnConfirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                stringName = Name.getText().toString();
                if(stringName.isEmpty()){
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage("Name cannot be left empty. Please insert your name");
                    alertDialog.setCancelable(false);
                    alertDialog.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) { 
                                    Name.setFocusable(true);
                                    Name.setFocusableInTouchMode(true);
                                    Name.requestFocus();
                                }
                            });

                    alertDialog.show();
                    Name.requestFocus();
                }
                else{
                    StringBuffer sbName = new StringBuffer();
                    sbName.append(stringName);
                    Intent intNext = new Intent(MainActivity.this,MainPage.class);
                    intNext.putExtra("Name",stringName.toString()); // i.putExtra only allow for String

                    startActivity(intNext);
                }

            }
        });

        layout.addView(tvName);
        layout.addView(Name);
        layout.addView(btnConfirm);
        setContentView(layout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的logcat

03-15 14:37:38.143: D/AndroidRuntime(7217): Shutting down VM
03-15 14:37:38.143: W/dalvikvm(7217): threadid=1: thread exiting with uncaught exception (group=0x55e04b20)
03-15 14:37:38.143: D/AndroidRuntime(7217): procName from cmdline: com.ytl.test
03-15 14:37:38.143: E/AndroidRuntime(7217): in writeCrashedAppName, pkgName :com.ytl.test
03-15 14:37:38.143: D/AndroidRuntime(7217): file written successfully with content: com.ytl.test StringBuffer : ;com.ytl.test
03-15 14:37:38.153: E/AndroidRuntime(7217): FATAL EXCEPTION: main
03-15 14:37:38.153: E/AndroidRuntime(7217): Process: com.ytl.test, PID: 7217
03-15 14:37:38.153: E/AndroidRuntime(7217): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ytl.test/com.ytl.test.MainActivity}: java.lang.NullPointerException
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.os.Looper.loop(Looper.java:136)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread.main(ActivityThread.java:5021)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at dalvik.system.NativeStart.main(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217): Caused by: java.lang.NullPointerException
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.view.View.<init>(View.java:3443)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.view.View.<init>(View.java:3510)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.widget.TextView.<init>(TextView.java:623)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.widget.Button.<init>(Button.java:107)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.widget.Button.<init>(Button.java:103)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.widget.Button.<init>(Button.java:99)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at com.ytl.test.MainActivity.<init>(MainActivity.java:22)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at java.lang.Class.newInstanceImpl(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at java.lang.Class.newInstance(Class.java:1208)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
03-15 14:37:38.153: E/AndroidRuntime(7217):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
03-15 14:37:38.153: E/AndroidRuntime(7217):     ... 11 more
03-15 14:37:38.213: D/AndroidRuntime(7230): Shutting down VM
03-15 14:37:38.213: W/dalvikvm(7230): threadid=1: thread exiting with uncaught exception (group=0x55e04b20)

提前感谢您的帮助

更新我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ytl.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".BackgroundService" >
        </service>

        <activity
            android:name=".MainPage"
            android:label="@string/title_activity_main_page" >
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

据我所知,问题在于这两行中的第一行(尽管如果允许执行,第二行也会导致相同的异常)......

private Button btnConfirm = new Button(this) ;
...
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

...您无法在this的主体中使用Activity,因为Activity尚未在该阶段完全实例化this private Button btnConfirm; ... AlertDialog.Builder alertDialog; 1}}将为空。

将它们更改为......

btnConfirm

您正在使用onCreate(...)方法实例化alertDialog = new AlertDialog.Builder(this); ,所以您需要做的就是添加一行,例如......

onCreate(...)

...也在{{1}}方法的某个地方。

答案 1 :(得分:0)

这是因为您在Button等于某事之前使用this - 关键字创建了新的this

您不能使用this之外的onCreate() - 关键字或属于该类成员的方法(非静态)。

解决方案只是初始化ButtonAlertDialog.Builder方法中的onCreate()