2015-08-14 72 views
1

我正在学习使用抽屉导航创建滑动导航的教程。错误:无法在android中实例化活动

Android Manifest.xml 

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

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="22" /> 

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

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

</manifest> 

下面是我MainActivity.java

package com.aa.slide; 

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

// Adopted from: https://developer.android.com/training/implementing-navigation/nav-drawer.html 
public class MainActivity extends ActionBarActivity { 

    private String[] mPlanetTitles; 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private CharSequence mTitle; 
    private ActionBarDrawerToggle mDrawerToggle; 

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

     mTitle = "test"; 

     mPlanetTitles = new String[]{"one", "two", "three"}; 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     // Set the adapter for the list view 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mPlanetTitles)); 
     // Set the list's click listener 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description */ 
       R.string.drawer_close /* "close drawer" description */ 
     ) { 

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

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

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

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

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

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

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

    /** 
    * Swaps fragments in the main content view 
    */ 
    private void selectItem(int position) { 
     Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show(); 

     // Highlight the selected item, update the title, and close the drawer 
     mDrawerList.setItemChecked(position, true); 
     setTitle(mPlanetTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     mTitle = title; 
     getSupportActionBar().setTitle(mTitle); 
    } 

    private class DrawerItemClickListener implements ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView parent, View view, int position, long id) { 
      selectItem(position); 
     } 
    } 

} 

在执行上面的代码,我一直在我的日志

08-14 13:18:18.632: W/dalvikvm(12483): Unable to resolve superclass of Lcom/aa/slide/MainActivity; (11) 
08-14 13:18:18.632: W/dalvikvm(12483): Link of class 'Lcom/aa/slide/MainActivity;' failed 
08-14 13:18:18.632: D/AndroidRuntime(12483): Shutting down VM 
08-14 13:18:18.632: W/dalvikvm(12483): threadid=1: thread exiting with uncaught exception (group=0x4113c2a0) 
08-14 13:18:18.637: E/AndroidRuntime(12483): FATAL EXCEPTION: main 
08-14 13:18:18.637: E/AndroidRuntime(12483): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aa.slide/com.aa.slide.MainActivity}: java.lang.ClassNotFoundException: com.aa.slide.MainActivity 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.access$700(ActivityThread.java:140) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.os.Looper.loop(Looper.java:137) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.main(ActivityThread.java:4921) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.reflect.Method.invokeNative(Native Method) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.reflect.Method.invoke(Method.java:511) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at dalvik.system.NativeStart.main(Native Method) 
08-14 13:18:18.637: E/AndroidRuntime(12483): Caused by: java.lang.ClassNotFoundException: com.aa.slide.MainActivity 
08-14 13:18:18.637: E/AndroidRuntime(12483): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.Instrumentation.newActivity(Instrumentation.java:1068) 
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025) 
08-14 13:18:18.637: E/AndroidRuntime(12483): ... 11 more 

UPDATE recieving这个错误,这是我的版本。 gradle

apply plugin: 'android' 

dependencies { 

    compile fileTree(dir: 'libs', include: '*.jar') 
    compile 'com.Android.support:appcompat-v7:21.0.+' 
} 

android { 
    compileSdkVersion 22 
    buildToolsVersion "22.0.1" 

    sourceSets { 
     main { 
      manifest.srcFile 'AndroidManifest.xml' 
      java.srcDirs = ['src'] 
      resources.srcDirs = ['src'] 
      aidl.srcDirs = ['src'] 
      renderscript.srcDirs = ['src'] 
      res.srcDirs = ['res'] 
      assets.srcDirs = ['assets'] 
     } 

     // Move the tests to tests/java, tests/res, etc... 
     instrumentTest.setRoot('tests') 

     // Move the build types to build-types/<type> 
     // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... 
     // This moves them out of them default location under src/<type>/... which would 
     // conflict with src/ being used by the main source set. 
     // Adding new build types or product flavors should be accompanied 
     // by a similar customization. 
     debug.setRoot('build-types/debug') 
     release.setRoot('build-types/release') 
    } 
} 

请问我该如何解决无法实例化活动的这个问题?由于

+0

您可以清理或重新构建一次项目。 –

+0

请显示您的build.gradle文件 – EpicPandaForce

+0

我已经添加了我的build.gradle文件 – NewBIe

回答

-1

试试这个清单文件:

**android:name=".MainActivity"** 

<activity 
     android:name="com.aa.slide.MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.MyCompatTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

请执行此操作以及 – NewBIe

+0

您好。在您的几个答案中,您已将代码格式应用于您的介绍性文字 - 您是否会避免这样做?如果仅将代码格式应用于代码,则它更具可读性。刚才看到我的编辑。谢谢。 – halfer

0

要确保这是您的gradle文件:

dependencies { 
    compile 'com.android.support:appcompat-v7:21.0.0' 
} 

的问题是,dalvikVM找不到import android.support.v7.app.ActionBarActivity;这是超的MainActivity

你可以看到这个在:

08-14 13:18:18.632: W/dalvikvm(12483): Unable to resolve superclass of Lcom/aa/slide/MainActivity; (11) 
+0

请我试过,但是,它仍然给出相同的错误 – NewBIe

1

什么异常抛出

java.lang.ClassNotFoundException: com.aa.slide.MainActivity

ClassNotFoundException时出现的类加载器无法找到类路径所需的类。所以,基本上你应该检查你的类路径,并在类路径中添加类。

重建和清理您的项目。请检查这SO答案。 对于您的信息,由于版本22.1.0,类别ActionBarActivity已被弃用。您应该使用AppCompatActivityActvity。我希望它能帮助你。

相关问题