AppCompat 和 Fragment 不起作用


02-19 11:49:17.369: E/AndroidRuntime(4209): java.lang.NoClassDefFoundError: com.slidingmenus.fragments.HomeFragment
02-19 11:49:17.369: E/AndroidRuntime(4209):     at com.slidingmenus.MainActivity.displayCategoryView(MainActivity.java:242)
02-19 11:49:17.369: E/AndroidRuntime(4209):     at com.slidingmenus.MainActivity.onCreate(MainActivity.java:121)

尝试了堆栈溢出中建议的每个步骤,但它们没有帮助。

它在 4.0+ 中没有错误,但在 2.3.x 设备中它给出了 java.lang.NoClassDefFoundError 在

line 242: fragment = new HomeFragment();

我从片段导入的是:

import android.app.Fragment;
import android.app.FragmentManager;

我正在使用:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

这是我的家庭片段:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;   
public class HomeFragment extends Fragment {        
   public HomeFragment(){}
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.layout_main_fragment,container, false);
           return rootView;
    }
 }

花了一整个上午的时间已经试图解决这个问题。 还是没有运气。任何帮助都非常感谢。

谢谢

您应该使用支持库中的Fragment

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

您还需要使用getSupportFragmentManager()并且由于您正在使用AppCompat因此您的活动必须扩展ActionbarActivity

 FragmentManager fragmentManager = getSupportFragmentManager();

更新:

ActionBarActivity已弃用AppCompatActivity支持库中的使用。不要忘记将您的支持存储库更新到最新版本。

试试这段代码进口:

import android.support.v4.app.Fragment;

并使用

    Fragment fragment = new HomeFragment();
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.layout.layout_main_fragment, fragment).commit();

我遇到了同样的问题,并通过导入支持库解决了它并使用了

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

相反

import android.app.Fragment;
import android.app.FragmentManager;

最新更新