[英]Android App Crashed - java.lang.IllegalArgumentException: No view found - Crash Report Added
I have developed a android application. 我已经开发了一个android应用程序。 I have an UI where I need a Fragment to be displayed in tabbed manner. 我有一个UI,需要片段以标签方式显示。 For compatibility I am using "TabHelper" from CompatTab classes provided by android forum. 为了兼容性,我使用了来自android论坛提供的CompatTab类中的“ TabHelper”。
Application crashes in most of major mobile models. 在大多数主要的移动型号中,应用程序崩溃。
APP_VERSION_CODE=30
ANDROID_VERSION=4.1.1
PHONE_MODEL=Galaxy Nexus
I also tried these links but did not get clues Android Fragment no view found for ID? 我也尝试了这些链接,但没有获得线索Android Fragment找不到ID的视图?
Fragment without a view crashes on configuration change 没有视图的片段在配置更改时崩溃
Here is my create method 这是我的创建方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
//adding tabs....
}
I have OnTab Click event listener as follow 我有如下的OnTab Click事件监听器
public static class InstantiatingTabListener implements CompatTabListener {
private final TabCompatActivity mActivity;
private final Class mClass;
public InstantiatingTabListener(TabCompatActivity activity, Class<? extends Fragment> cls) {
mActivity = activity;
mClass = cls;
}
/* The following are each of the ActionBar.TabListener callbacks */
@Override
public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
Fragment fragment = tab.getFragment();
if (fragment == null) {
// If not, instantiate and add it to the activity
fragment = Fragment.instantiate(mActivity, mClass.getName());
tab.setFragment(fragment);
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
} else {
// If it exists, simply attach it in order to show it
ft.attach(fragment);
}
}
My XML TabHost 我的XML TabHost
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
It works fine in Samsung Galaxy Tablet but fails in Galaxy S2,S3 Mobiles. 它在Samsung Galaxy Tablet上工作正常,但在Galaxy S2,S3 Mobiles上失败。
Here is the crash report 这是当机报告
APP_VERSION_CODE=30
ANDROID_VERSION=4.1.1
PHONE_MODEL=Galaxy Nexus
CUSTOM_DATA=
STACK_TRACE=java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.ishare.guide/com.ishare.guide.GuideBrowserActivity}:
java.lang.IllegalArgumentException: No view found for id 0x1020011 for fragment
GuideMenuFragment{41ee9418 #0 id=0x1020011 menu}
I FEEL BELOW LINE OF CODE CREATES THE ISSUE 我觉得下面的代码创建了问题
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
I found below code in stack overflow but how to intergrate in my code? 我在堆栈溢出中找到以下代码,但是如何整合我的代码?
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
fragmentTransaction.commit();
The id which you pass into ft.add(), must be a child of the layout specified in setContentView()
. 您传递给ft.add()的ID必须是setContentView()
指定的布局的子级。 I think you have wrongly initialized your setContentView()
of the onCreate()
. 我认为您错误地初始化了onCreate()
setContentView()
onCreate()
。 Also make sure the ID which you are passing to FT is unique. 还要确保您传递给FT的ID是唯一的。
There are helpful resources on developer.android.com which describe horizontal navigation: 在developer.android.com上有一些有用的资源,它们描述了水平导航:
Check Implementing Lateral Navigation for horizontal and also tab navigation. 选中“ 实施横向导航”以进行水平导航以及选项卡导航。
There is a backward compatibility package from android which supports this functionality. android有一个向后兼容的软件包,它支持此功能。 Check android.support.v4.app 检查android.support.v4.app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.