Android AppCompat 和 Fragment 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21873114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AppCompat and Fragment not working
提问by prashantwosti
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)
Tried each and every steps suggested in stack overflow but they didn't help.
尝试了堆栈溢出中建议的每个步骤,但没有帮助。
It works without an error in 4.0+ but in 2.3.x devices its giving java.lang.NoClassDefFoundError in
它在 4.0+ 中没有错误,但在 2.3.x 设备中它给出了 java.lang.NoClassDefFoundError
line 242: fragment = new HomeFragment();
My imports from fragments are:
我从片段的进口是:
import android.app.Fragment;
import android.app.FragmentManager;
and I'm using:
我正在使用:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
Here is my home fragment:
这是我的家片段:
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;
}
}
Spent whole morning already trying to solve this. still no luck. Any help is highly appreciated.
整个上午都在试图解决这个问题。仍然没有运气。任何帮助都受到高度赞赏。
Thanks
谢谢
回答by Raghunandan
You should use Fragment
from the support library.
您应该Fragment
从支持库中使用。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
You also need to use getSupportFragmentManager()
and since you are using AppCompat
your activity must extend ActionbarActivity
.
您还需要使用getSupportFragmentManager()
并且因为您正在使用AppCompat
您的活动必须扩展ActionbarActivity
.
FragmentManager fragmentManager = getSupportFragmentManager();
Update:
更新:
ActionBarActivity
is deprecated use AppCompatActivity
from support library. Don't forget to update your support repository to the latest.
ActionBarActivity
不推荐使用AppCompatActivity
支持库。不要忘记将您的支持存储库更新到最新版本。
回答by LONGMAN
Try this code Import:
试试这个代码导入:
import android.support.v4.app.Fragment;
And use
并使用
Fragment fragment = new HomeFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.layout.layout_main_fragment, fragment).commit();
回答by M D
I faced the same issue and resolved it by import support libraryand used
我遇到了同样的问题并通过导入支持库解决了它并使用了
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Instead
反而
import android.app.Fragment;
import android.app.FragmentManager;