安卓 - java.lang.RuntimeException : Unable to instantiate activity

标签 android android-fragments android-activity android-manifest runtime-error

我正在尝试运行我的程序,但出现标题中列出的错误,我无法弄清楚原因

下面是我的MainActivity

package com.example.iwould;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.iwould.R;

// GOING TO HAVE TO MAKE THE BODY THE MAIN PAGE

public class MainActivity extends FragmentActivity {
private String[] mPlanetTitles;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActionBar().setDisplayShowCustomEnabled(true);
    getActionBar().setDisplayShowTitleEnabled(false);

    LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.titleview, null);

    //if you need to customize anything else about the text, do it here.
    //I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
    ((TextView)v.findViewById(R.id.title)).setText(getTitle());
    Typeface typeface = Typeface.createFromAsset(this.getAssets(), "robotomed.ttf");
    ((TextView)v.findViewById(R.id.title)).setTypeface(typeface);
    //assign the view to the actionbar
    getActionBar().setCustomView(v);

    setContentView(R.layout.navdrawer);

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    Home fragment = new Home();
    fragmentTransaction.replace(R.id.llhome, fragment);
    fragmentTransaction.commit();

    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    CustomListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.drawer_list_item , mPlanetTitles);

    mDrawerList.setAdapter(listAdapter);
    // Set the list's click listener

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
    R.string.app_name, /* "open drawer" description */
    R.string.about_us /* "close drawer" description */
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("Home");
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("StayHealthy");
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);



}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}


private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position,
            long id) {
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fragmentManager
                .beginTransaction();
        selectItem(position);
        switch (position) {
        case (0):
            Home HomeFragment = new Home();
        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.llhome, HomeFragment);
        transaction.addToBackStack(null);
        // Commit the transaction
        transaction.commit();
              mDrawerList.setItemChecked(position, true);
                mDrawerLayout.closeDrawer(mDrawerList);
            break;
        case (1):

            break;
        case (2):

            break;
        case (3):
            Toast.makeText(getApplicationContext(), "Not Configured",
                    Toast.LENGTH_SHORT).show();
            break;
        case (4):
            Toast.makeText(getApplicationContext(), "Not Configured",
                    Toast.LENGTH_SHORT).show();
            break;
        }
        mDrawerList.clearChoices();
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {

}

@Override
public void setTitle(CharSequence title) {
    String mTitle = "Title";
    getActionBar().setTitle(mTitle);
}

private class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private String[] items ;

    public CustomListAdapter(Context context, int textViewResourceId , String[] list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.tvmenu);
        Typeface tf = Typeface.createFromAsset(getAssets(),"aristareg.ttf");

        if(items[position] != null )
        {


            text.setTypeface(tf);
            //text.setTextColor(Color.WHITE);
            text.setText(items[position]);
            //text.setBackgroundColor(Color.RED); 
            int color = Color.rgb( 51, 153, 255);
                text.setTextColor( color );

        }

        if(position == 3){
             int color = Color.rgb( 34, 153, 34);
                text.setTextColor( color );
        }

        return mView;
    }

}

}

这是我的 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.app.iWould"
package="com.app.iwould"
android:versionCode="3"
android:versionName="1.2" >

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.example.iwould.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="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>

</manifest>

下面是我的logcat

07-25 00:34:00.099: E/AndroidRuntime(4535): FATAL EXCEPTION: main
07-25 00:34:00.099: E/AndroidRuntime(4535): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.iwould/com.example.iwould.MainActivity}: java.lang.ClassNotFoundException: com.example.iwould.MainActivity
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.os.Looper.loop(Looper.java:137)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at  android.app.ActivityThread.main(ActivityThread.java:4575)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at java.lang.reflect.Method.invoke(Method.java:511)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at dalvik.system.NativeStart.main(Native Method)
07-25 00:34:00.099: E/AndroidRuntime(4535): Caused by: java.lang.ClassNotFoundException: com.example.iwould.MainActivity
07-25 00:34:00.099: E/AndroidRuntime(4535):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-25 00:34:00.099: E/AndroidRuntime(4535):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984)
07-25 00:34:00.099: E/AndroidRuntime(4535):     ... 11 more

如果有人能弄清楚为什么这不起作用,那就太好了。在 MainActivity 中,我正在实现一个抽屉导航,所以我从扩展一个 Activity 到扩展一个 FragmentActivity。我认为这可能与此有关。

最佳答案

list 中的包裹

package="com.app.iwould"

&你的包裹是

com.example.iwould.MainActivity

这就是你得到“ClassNotFound”异常的原因。

关于安卓 - java.lang.RuntimeException : Unable to instantiate activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849285/

相关文章:

android - java.lang.NoClassDefFoundError : com. parse.Parse$Configuration$Builder 在以下 Lollipop 版本上

android - 假 NFC(近场通信)启动的可能性

android - 如何在Android库(AAR)中包含Jar文件的许可证

java - 在 fragment 上实现向上导航

android - RadioGroup 不能在 fragment android 中工作

startActivityForResult 返回后未重新创建 Android parentActivity

android - 显示设置并在首次应用程序启动时记住

android - onActivityResult 的问题

android - Clean Architecture 中具有多个响应列表的用例

java - 如何将变量放入字符串资源中?