相关疑难解决方法(0)

无法强制转换为android.app.Fragment

我只是想看看片段,我得到了一些愚蠢的东西...有我的SkippersActivity.java:

public class SkippersActivity extends Activity{
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.skippers_fragment);
 }
}
Run Code Online (Sandbox Code Playgroud)

还有我的skippers_fragment.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.arkezis.globedroid.SkippersFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/skippers_fragment">
    </fragment>
Run Code Online (Sandbox Code Playgroud)

还有我的SkippersFragment.xml:

public class SkippersFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View mainView = inflater.inflate(R.layout.skippers, container, false);      
    return mainView;
}   
Run Code Online (Sandbox Code Playgroud)

}

我的skippers.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    
    <ListView android:id="@+id/list_skippers_all"
        android:layout_width="match_parent"
    android:layout_height="match_parent">
    </ListView>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

为什么我得到:

04-30 13:45:16.355: E/AndroidRuntime(30077): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arkezis.globedroid/com.arkezis.globedroid.SkippersActivity}: android.view.InflateException: Binary XML …
Run Code Online (Sandbox Code Playgroud)

android

31
推荐指数
2
解决办法
4万
查看次数

用兼容包android充气片段

我试图给包含Fragment使用向后兼容包的布局充气.我把jar文件放在我项目的libs文件夹中.我扩展了Fragment,然后尝试通过将Activity的contentView设置为来扩充它

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
 <fragment
  class="com.test.fragments.AdFragment"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但是当我设置内容视图时,它会ClassNotFoundException因为片段标记而失败.这是logcat输出.

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: \
      android.view.InflateException: Binary XML file line #51: \
      Error inflating class fragment
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1793)
   at android.app.ActivityThread.access$1500(ActivityThread.java:123)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3848)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
   at dalvik.system.NativeStart.main(Native Method)

 Caused by: android.view.InflateException: Binary XML file line #51: \
      Error inflating class fragment
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:320) …
Run Code Online (Sandbox Code Playgroud)

android backwards-compatibility android-inflate android-fragments

26
推荐指数
1
解决办法
2万
查看次数

使用findFragmentById的ClassCastException

我尝试使用以下内容获取特定片段:

RohwareFragmentMatlist fragment =    (RohwareFragmentMatlist)getFragmentManager().findFragmentById(R.id.lagerfragment);
Run Code Online (Sandbox Code Playgroud)

但是我收到了来自eclipse的错误消息:

无法从Fragment转换为RohwareFragmentMatlist

活动开始于:

public class RohwareActionBar extends FragmentActivity {...
Run Code Online (Sandbox Code Playgroud)

RohwareFragmentMatlist定义如下:

public class RohwareFragmentMatlist extends ListFragment
        implements LoaderManager.LoaderCallbacks<Cursor>{...
Run Code Online (Sandbox Code Playgroud)

Fragment以这种方式定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" 
android:layout_height="match_parent">

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="de.ypssoft.RohwareFragmentMatlist"
android:layout_weight="1"
android:layout_width="0px" 
android:layout_height="match_parent"
android:id="@+id/lagerfragment" 
android:layout_margin="5dip" 
android:layout_marginLeft="10dip"
>
</fragment>
<FrameLayout 
android:id="@+id/details" 
android:layout_weight="3"
android:layout_width="0px" 
android:layout_height="match_parent"
 android:background="?android:attr/detailsElementBackground" /> 

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

使用ListFragment通过"getFragmentById"获取片段不起作用吗?

android-fragments

7
推荐指数
1
解决办法
4910
查看次数

Android片段没有显示出来

我的android应用程序中有以下代码:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="activity" />

<fragment
    android:id="@+id/list"
    android:name="com.hugo.test.FragmentA"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

fragmenta.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="helloA" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

FragmentA.java:

package com.hugo.test;

//some imports here

public class FragmentA extends Fragment {
    /** Called when the activity is first created. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        //super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        return inflater.inflate(R.layout.fragmenta, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentActivity.java: …

android fragment

1
推荐指数
1
解决办法
7164
查看次数