简体   繁体   中英

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo even with android:name

As many beginners it seems, I am getting java.lang.RuntimeException: Unable to instantiate activity ComponentInfo and the AVD says my app has stopped. I saw that the common fix for this is making sure your activity is there and named, but I think I already did this. Here is my manifest -

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity
            android:name="com.meacrux.MeacruxActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

As you can see I have <application <activity android:name=..../>/> , but I still get the issue. What I noticed is that I for some reason also have an intent-filter tag. I noticed that when I take the intent-filter tag out and run the app it doesn't cause any exceptions, but it also doesn't start in the AVD. So how can I fix this?

Edit: I forgot to mention that the error also has a NPE . Here is the full message:

E/AndroidRuntime(2189): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.meacrux/com.meacrux.MeacruxActivity}: java.lang.NullPointerException

Edit2: Here is the code -

package com.meacrux;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MeacruxActivity extends FragmentActivity {
    private static final int    BACKGROUND_MENU_ID = Menu.FIRST,
                                CROSS_MENU_ID = Menu.FIRST + 1,
                                SAVE_MENU_ID = Menu.FIRST+2;
    private static final String logID = "Mx";
    private static String[] backgrounds;

    //private ImageView imageview = new ImageView(getApplicationContext());
    private Dialog currentDialog;
    private Toast notImplementedToast = Toast.makeText(getApplicationContext(),  R.string.not_implemented, Toast.LENGTH_SHORT);

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meacrux);

        backgrounds =  getResources().getStringArray(R.array.background_options);

        notImplementedToast.setGravity(Gravity.CENTER, notImplementedToast.getXOffset() / 2, notImplementedToast.getYOffset() / 2);

        // Set up the action bar to show a dropdown list.
        /*final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
        // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(getActionBarThemedContextCompat(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1, new String[] {
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3), }), this);*/
    }

    /**
     * Backward-compatible version of {@link ActionBar#getThemedContext()} that
     * simply returns the {@link android.app.Activity} if
     * <code>getThemedContext</code> is unavailable.
     */
    /*@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private Context getActionBarThemedContextCompat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return getActionBar().getThemedContext();
        } else {
            return this;
        }
    }*/

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
                .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(Menu.NONE, BACKGROUND_MENU_ID, Menu.NONE, R.string.menuitem_background);
        menu.add(Menu.NONE, CROSS_MENU_ID, Menu.NONE, R.string.menuitem_cross);
        menu.add(Menu.NONE, SAVE_MENU_ID, Menu.NONE, R.string.menuitem_save);

        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.meacrux, menu);
        return true;
    }

    @Override
     public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case BACKGROUND_MENU_ID:
            showBackgrounDialog();
            break;

        case CROSS_MENU_ID:
            showCrossDialog();
            break;

        case SAVE_MENU_ID:
            save();
            break;
        }
        return true;
    }

    private void showBackgrounDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MeacruxActivity.this);
        builder.setTitle(R.string.background_dialog_title).setCancelable(true)
                .setItems(R.array.background_options, 
                        new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int selection) {
                                Log.d(logID, "the selection is: " + selection);
                                if(backgrounds.length==selection){
                                    notImplementedToast.show();
                                    return;
                                }
                                setBackground(backgrounds[selection]);  
                            }
                        });

        currentDialog = builder.create();   
    }

    private void setBackground(String string) {
        // TODO Auto-generated method stub

    }   

    private void showCrossDialog() {
        // TODO Auto-generated method stub

    }

    private void save() {
        // TODO Auto-generated method stub

    }
}

Looks like the NPE is because you are trying to create instantiate a Toast as memeber.

Paste this inside your onCreate

Toast notImplementedToast = Toast.makeText(getApplicationContext(), R.string.not_implemented, Toast.LENGTH_SHORT);

and remove it from the top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM