导入v7 app compact后出错

时间:2015-09-18 12:52:43

标签: android appcompat-v7-r23

执行代码时

package com.example.swvt1;
import java.util.ArrayList;
import java.util.List;
import com.example.adapters.MyFragmentPagerAdapter;
import com.example.fragments.fragment1;
import com.example.fragments.fragment2;
import com.example.fragments.fragment3;
import com.example.fragments.fragment4;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {  
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    List<Fragment> listfragments = new ArrayList<Fragment>();
    listfragments.add(new fragment1()); 
    listfragments.add(new fragment2());
    listfragments.add(new fragment3());
    listfragments.add(new fragment4());

    MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), listfragments);
viewPager.setAdapter(myFragmentPagerAdapter);
}

@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
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

执行上面的代码时,我得到的错误如找不到类定义和类未找到异常,堆栈跟踪如下

E/AndroidRuntime(27466): FATAL EXCEPTION: main
E/AndroidRuntime(27466): Process: com.example.swvt1, PID: 27466
E/AndroidRuntime(27466): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.swvt1/com.example.swvt1.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.swvt1.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.swvt1-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(27466):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2290)
E/AndroidRuntime(27466):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
E/AndroidRuntime(27466):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
E/AndroidRuntime(27466):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
E/AndroidRuntime(27466):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(27466):    at android.os.Looper.loop(Looper.java:211)
E/AndroidRuntime(27466):    at android.app.ActivityThread.main(ActivityThread.java:5371)
E/AndroidRuntime(27466):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27466):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(27466):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
E/AndroidRuntime(27466):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
E/AndroidRuntime(27466): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.swvt1.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.swvt1-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(27466):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(27466):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(27466):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(27466):    at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
E/AndroidRuntime(27466):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
E/AndroidRuntime(27466):    ... 10 more
E/AndroidRuntime(27466):    Suppressed: java.lang.NoClassDefFoundError: com.example.swvt1.MainActivity
E/AndroidRuntime(27466):        at dalvik.system.DexFile.defineClassNative(Native Method)
E/AndroidRuntime(27466):        at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/AndroidRuntime(27466):        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/AndroidRuntime(27466):        at dalvik.system.DexPathList.findClass(DexPathList.java:321)
E/AndroidRuntime(27466):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/AndroidRuntime(27466):        ... 14 more
E/AndroidRuntime(27466):    Suppressed: java.lang.ClassNotFoundException: com.example.swvt1.MainActivity
E/AndroidRuntime(27466):        at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(27466):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(27466):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(27466):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(27466):        ... 13 more
E/AndroidRuntime(27466):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我做了什么错误并提前致谢

1 个答案:

答案 0 :(得分:0)

进入&#34; Java Build Path&#34; - &GT;订单和出口 - &gt;选中所有复选框 - &gt;干净 - &gt;构建 - &gt;运行

确保您的图书馆中有v7