1

我正在尝试运行我的程序,但我收到了标题中列出的错误,我不知道为什么

下面是我的主要活动

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

}

}

这是我的清单

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

下面是我的日志

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 中,我正在实现一个导航抽屉,所以我从扩展一个活动到扩展一个 FragmentActivity。我认为这可能与它有关。

4

4 回答 4

3

您在清单中的包裹

package="com.app.iwould"

你的包裹是

com.example.iwould.MainActivity

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

于 2013-07-25T04:58:40.047 回答
1

原来这是 Android SDK Tools r17 的问题

right-click on your project in Eclipse and choose Android Tools-->Add Support Library...

参考这里

于 2013-07-25T05:02:40.300 回答
0

确保您的项目或它用作库的任何项目都没有支持 v13 jar。

于 2014-07-10T09:13:10.410 回答
0

将清单中的活动名称更改为

<activity android:name="com.app.iwould.MainActivity"

您目前在清单中有单词example而不是app

还将您的活动定义为的包重命名为com.app.iwould(在 MainActivity.java 中)

于 2013-07-25T05:03:24.020 回答