0

正如许多初学者一样,我得到了java.lang.RuntimeException: Unable to instantiate activity ComponentInfo,并且 AVD 说我的应用程序已停止。我看到解决此问题的常见方法是确保您的活动存在并命名,但我想我已经这样做了。这是我的清单-

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

如您所见,我有<application <activity android:name=..../>/>,但我仍然遇到问题。我注意到的是,出于某种原因,我也有一个intent-filter标签。我注意到,当我取出intent-filter标签并运行应用程序时,它不会导致任何异常,但它也不会在 AVD 中启动。那么我该如何解决这个问题呢?

编辑:我忘了提到错误也有一个NPE. 这是完整的消息:

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

Edit2:这是代码 -

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

    }
}
4

1 回答 1

0

看起来NPE是因为您正在尝试创建实例化 aToast作为成员。

将此粘贴到您的onCreate

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

并将其从顶部移除。

于 2013-07-19T21:35:58.493 回答