获取错误致命java.lang.RuntimeException:无法启动活动

时间:2014-03-13 23:00:25

标签: java android runtime-error

我是Android编程新手,只有一点java背景。出于某种原因,我在基于The New Bostons Android Tutorial系列制作的程序中的3个类创建了类似于此的错误:

03-13 18:17:22.095: E/AndroidRuntime(13055): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infitenothing.dogcat/com.infitenothing.word.OpenClass}: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.os.Looper.loop(Looper.java:136)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at java.lang.reflect.Method.invoke(Method.java:515)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at dalvik.system.NativeStart.main(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055): Caused by: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055):    at com.infitenothing.word.OpenClass.intalize(OpenClass.java:43)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at com.infitenothing.word.OpenClass.onCreate(OpenClass.java:29)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.Activity.performCreate(Activity.java:5231)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 18:17:22.095: E/AndroidRuntime(13055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-13 18:17:22.095: E/AndroidRuntime(13055):    ... 12 more

我使用此类启动活动

 public class Menu extends ListActivity {

String classes[] = {"MainActivity", "Splash", "TextPlay", "Email", "Camera", "DatA", "OpenClass", "example1", "example1"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes ));


}




@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub

    super.onListItemClick(l, v, position, id);
    String cheese = classes[position];
    try{
    Class ourClass = Class.forName("com.infitenothing.word." + cheese);
    Intent ourIntent = new Intent(Menu.this, ourClass);
    startActivity(ourIntent);
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}

}

这些是无效的课程

package com.infitenothing.word;



import com.infitenothing.dogcat.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Data extends Activity implements OnClickListener {

Button start, startFor;
TextView gotAnswer;
EditText  sendET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    intalize();
    setContentView(R.layout.get);

}

private void intalize() {
    start = (Button) findViewById(R.id.bSA);
    startFor = (Button) findViewById(R.id.bSAFR);
    gotAnswer = (TextView) findViewById(R.id.tvGot);
    sendET= (EditText) findViewById(R.id.etSend);
    start.setOnClickListener(this);
    startFor.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()){
    case R.id.bSA:
        String bread = sendET.getText().toString();
        Bundle basket = new Bundle();
        basket.putString("key", bread);
        Intent a = new Intent(Data.this, OpenClass.class);
        a.putExtras(basket);
        startActivity(a);
        break;

    case R.id.bSAFR:



        break;

        }
}

}

package com.infitenothing.word;

import java.util.Random;

import com.infitenothing.dogcat.R;

import android.R.anim;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity implements View.OnClickListener {

Button chkCmd;
ToggleButton passTog;
EditText input;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);

    baconAndEggs();

    passTog.setOnClickListener(this);
    chkCmd.setOnClickListener(this);
}

private void baconAndEggs() {
    // TODO Auto-generated method stub
    Button chkCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);

}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    switch (view.getId()) {
    case R.id.bResults:
        // TODO Auto-generated method stub
        String check = input.getText().toString();
        if (check.contentEquals("left") || check.contentEquals("Left")) {
            display.setText("LEFT!!!");
            display.setGravity(Gravity.LEFT);

        } else if (check.contentEquals("center")
                || check.contentEquals("Center")) {
            display.setText(check + " !!!");
            display.setGravity(Gravity.CENTER);

        } else if (check.contentEquals("right")
                || check.contentEquals("Right")) {
            display.setText("Ha Ha you have been tricked");
            display.setGravity(Gravity.LEFT);

        } else if (check.contentEquals("blue")
                || check.contentEquals("Blue")) {
            display.setText("Yellow");
            display.setTextColor(Color.BLUE);
            display.setTextSize(50);

        } else if (check.contentEquals("role")) {
            // Random
            // Way one (created by me) Num choser
            /*
             * double joe = Math.random(); double crazy = joe *100; int cast
             * = (int) crazy; display.setText("" + cast);
             */

            // Way two Travis
            Random crazyRandom = new Random();
            display.setText("Dice???????!!!!!!");
            display.setTextSize(crazyRandom.nextInt(200));
            display.setTextColor(Color.rgb(crazyRandom.nextInt(256),
                    crazyRandom.nextInt(256), crazyRandom.nextInt(256)));

            // Switch Travis
            switch (crazyRandom.nextInt(3)) {
            case 0:
                display.setGravity(Gravity.LEFT);
                break;
            case 1:
                display.setGravity(Gravity.CENTER);
                break;
            case 2:
                display.setGravity(Gravity.RIGHT);
                break;

            }

        } else if (check.contentEquals("cat")) {
            MediaPlayer meow = MediaPlayer
                    .create(TextPlay.this, R.raw.meow);
            display.setText("I am the most evil cat");
            meow.start();
        } else {
            display.setGravity(Gravity.CENTER);
            display.setTextColor(Color.WHITE);
            display.setText("invalid");
        }

        break;
    case R.id.tbPassword:
        if (passTog.isChecked() == true) {
            input.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            input.setInputType(InputType.TYPE_CLASS_TEXT);
        }

        break;

    }
}

}

最后

  package com.infitenothing.word;

    import com.infitenothing.dogcat.R;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;

public class OpenClass extends Activity implements OnClickListener,
        OnCheckedChangeListener {

    TextView question, test;
    Button returnData;
    RadioGroup selectList;
    String gotBread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);
        intalize();
        /*
         * Bundle gotBasket = getIntent().getExtras(); gotBread =
         * gotBasket.getString("key"); question.setText(gotBread);
         */

    }

    private void intalize() {
        // TODO Auto-generated method stub
        question = (TextView) findViewById(R.id.tvQestion);
        question = (TextView) findViewById(R.id.tvQestion);
        selectList = (RadioGroup) findViewById(R.id.rgAnswer);
        selectList.setOnCheckedChangeListener(this);

        returnData.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        // check id ma not be right vabable
        switch (checkedId) {
        case R.id.rcrazy:

            break;

        case R.id.rawsome:

            break;

        case R.id.rBoth:

            break;
        }

    }

}

我的机器人清单是

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <permission android:name="android.permission.SET_WALLPAPER"></permission>

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

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.infitenothing.word.Menu"
            android:label="SPLASH" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.infitenothing.word.Splash"
            android:label="SPLASH" >
            <intent-filter>
                <action android:name="com.infitenothing.word.Splash" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.infitenothing.word.Magic"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.infitenothing.word.Magic" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.infitenothing.word.TextPlay"
            android:label="@string/app_name" >

        </activity>

         <activity
            android:name="com.infitenothing.word.Email"
            android:label="@string/app_name" >

        </activity>

         <activity
            android:name="com.infitenothing.word.Camera"
            android:label="Camera to Wallpaper" 
            android:screenOrientation="portrait"
            >

        </activity>
        <activity
            android:name="com.infitenothing.word.Data"
            android:label="Camera to Wallpaper" 
            android:screenOrientation="portrait"

            >
             <intent-filter>
                <action android:name="com.infitenothing.word.Data" />

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

        </activity>
        <activity
            android:name="com.infitenothing.word.OpenClass"
            android:label="Camera to Wallpaper" 
            android:screenOrientation="portrait"
            >

        </activity>

    </application>

</manifest>

很抱歉这篇文章很长,但我一直在寻找一段时间,并没有找到任何解决这个问题的好答案。

解决此问题的任何答案都将是一个巨大的帮助

1 个答案:

答案 0 :(得分:1)

您正在按钮上调用setOnClickListener方法:

returnData.setOnClickListener(this);

但您之前从未初始化此按钮! ==&GT; NPE

要解决此问题,您需要像使用其他视图一样使用findViewById:

returnData = (Button) findViewById(R.id.the_id_of_your_button);