【问题标题】:class not found on path在路径上找不到类
【发布时间】:2013-12-17 00:32:39
【问题描述】:

我正在使用适用于 android 的 facebook sdk,并且正在学习教程。当我运行我的代码时,它显示 java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.example.ftester/com.example.ftester.MainActivity}:java.lang.ClassNotFoundException:找不到类“com.example. ftester.MainActivity" 在路径上:DexPathList[[zip file "/data/app/com.example.ftester-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ftester-2, /vendor /lib, /system/lib]]

    public class MainActivity extends FragmentActivity {


private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION +1;
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];

private Session.StatusCallback callback = 
        new Session.StatusCallback() {
        @Override
        public void call(Session session, 
                SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     FragmentManager fm = getSupportFragmentManager();
        fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
        fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

        FragmentTransaction transaction = fm.beginTransaction();
        for(int i = 0; i < fragments.length; i++) {
            transaction.hide(fragments[i]);
        }
        transaction.commit();


}





private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}


@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 onResumeFragments() {
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open,
        // try to show the selection fragment
        showFragment(SELECTION, false);
    } else {
        // otherwise present the splash screen
        // and ask the person to login.
        showFragment(SPLASH, false);
    }
 }

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    // Only make changes if the activity is visible
    if (isResumed) {
         FragmentManager manager = getSupportFragmentManager();
        // Get the number of entries in the back stack
        int backStackSize = manager.getBackStackEntryCount();
        // Clear the back stack
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        if (state.isOpened()) {
            // If the session state is open:
            // Show the authenticated fragment
            showFragment(SELECTION, false);
        } else if (state.isClosed()) {
            // If the session state is closed:
            // Show the login fragment
            showFragment(SPLASH, false);
        }
    }
}
@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    isResumed = true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}






@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
    isResumed = false;
}
     }




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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.ftester.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>
    </application>

   </manifest>

【问题讨论】:

  • 是否在 Manifest 文件中正确声明了 MainActivity?
  • 是的,我已经声明了
  • MainActivity 所在的包名是什么?
  • 包名是“com.example.ftester”
  • 你确定你的包名在你的 MainActivity.java 中是这样指定的吗?我在您粘贴的源代码中没有看到该行。无论如何,您收到此错误的唯一原因是 MainActivity.class 由于某种原因对系统不可见。从一个全新的简单 hello-world 项目重新开始。确保它正常工作,然后开始将所有片段代码添加到新项目中。这应该会有所帮助:)

标签: android classnotfoundexception


【解决方案1】:

您的 MainActivity.java 文件可能没有被编译并且在 apk 文件中不可用。检查并尝试此解决方案https://stackoverflow.com/a/18533212/3025732

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 2017-09-12
    • 2014-05-29
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2014-01-22
    相关资源
    最近更新 更多