运行应用程序时“无法实例化活动”

时间:2014-12-22 07:09:50

标签: java android

我在模拟器中启动应用程序时遇到问题。当我运行它时,我收到消息“抱歉MyApp已意外停止”

这就是我在LogCat中获得的内容:

12-22 06:59:42.196: D/AndroidRuntime(1083): Shutting down VM
12-22 06:59:42.196: W/dalvikvm(1083): threadid=1: thread exiting with uncaught exception (group=0xb60294f0)
12-22 06:59:42.285: E/AndroidRuntime(1083): FATAL EXCEPTION: main
12-22 06:59:42.285: E/AndroidRuntime(1083): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld5/com.example.helloworld5.MainActivity}: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.os.Looper.loop(Looper.java:130)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.reflect.Method.invoke(Method.java:507)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at dalvik.system.NativeStart.main(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.Class.newInstanceImpl(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.Class.newInstance(Class.java:1409)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-22 06:59:42.285: E/AndroidRuntime(1083):     ... 11 more

所以我查看了我的Manifest文件并更改了

<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>
<activity

为:

<activity
     android:name=".MainActivity"
     android:label="@string/app_name" >
     <intent-filter>
         <action android:name="com.example.helloworld5.MAIN"/>

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

但是当我在更改后运行它时,我得到No launch activity found!

这是我的清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        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>
        <activity
            android:name=".Floor"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld5.FLOOR" />

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

</manifest>

这是我的MainActivity代码:

MainActivity.java

package com.example.helloworld5;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

    //String name;
    DrawView drawView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       final  EditText dest = (EditText)findViewById(R.id.y_editText2);
        //final String roomName = dest.getText().toString();


        final Button openFloor = (Button)findViewById(R.id.y_button1);
        openFloor.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                 drawView.setRoomName(dest.getText().toString());

startActivity(new Intent("com.example.helloworld5.FLOOR"));
            }
        });
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

DrawView.java:

public class DrawView extends View {
    Paint paint = new Paint();
    float ux, dx, rx,lx;
    String roomName2 ;

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.RED);
        //roomName2 = drawView.getTag();
    }

   public void setRoomName(String name) {
         this.roomName2 = name;
    }

    public String getRoomName(){
        return roomName2;
    }
    public void setCoordinates(){
};
    @Override
    public void onDraw(Canvas canvas) {
        String roomName = getRoomName(); 
        if(roomName == "C154"){

            ux =90;
            dx = 250;
            rx = 90; 
            lx = 400;
        }else {
            ux =76;
            dx = 98;
            rx = 140; 
        lx = 300;

        }
            canvas.drawLine(90, 250 , 90, 400, paint);
            canvas.drawLine(20, 0, 0, 20, paint);
            canvas.drawCircle(150, 400, 30, paint);
    }

}

如何修复此崩溃?

4 个答案:

答案 0 :(得分:2)

你应该移动

EditText dest = (EditText)findViewById(R.id.y_editText2);
onCreate(...)

之后的setContentView(...)

答案 1 :(得分:2)

根据这个

12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.Class.newInstanceImpl(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at java.lang.Class.newInstance(Class.java:1409)

您正在活动的构造函数中调用findViewById。然而,到那时,布局还没有膨胀。将方法调用移至onCreate

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_main);
    dest = (EditText)findViewById(R.id.y_editText2);
    //et cetera
} 

编辑:

此外,您现在正在为空引用设置String roomName = dest.getText().toString();。像这样设置onClickListener:

openFloor.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        drawView.setRoomName(dest.getText().toString);
        startActivity(new Intent("com.example.helloworld5.FLOOR"));
    }
});

EDIT2:你的第二个NPE是因为DrawView没有初始化,也是初始化它:

public void onCreate(Bundle b) {
    super.onCreate(b);
    //...your other stuff
    this.drawView = (DrawView) findViewById(R.id.myDrawView);
}

答案 2 :(得分:1)

问题是:

12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083):     at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083):     at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)

EditText dest = (EditText)findViewById(R.id.y_editText2);

setContentView

之后

这只会解决目前的崩溃问题。

<强> PLUS

把你的

String roomName = dest.getText().toString();
按钮点击通话

如果您将其保留在全局变量部分中,它会为NullPointerException提供dest。如果你在onCreate之后的findViewById中放置这一行,它就会给你roomName =“”,就像那时刚刚初始化了视图一样。用户输入的值不会出现。

答案 3 :(得分:0)

您的活动应该是这样的。

public class MainActivity extends ActionBarActivity {

    DrawView drawView;
    String roomName;
    EditText dest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dest = (EditText)findViewById(R.id.y_editText2);
        final Button openFloor = (Button)findViewById(R.id.y_button1);
        openFloor.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                roomName = dest.getText().toString();

                   drawView.setRoomName(roomName);

       startActivity(new Intent("com.example.helloworld5.FLOOR"));
            }
        });
    }
}