0

我已经访问了很多类似的链接,即1、2 遵循各种示例/链接,即这个这个主要我已经多次关注Vogella的这篇文章,每次我得到同样的异常......可能是什么原因 ?

07-27 12:00:33.431: E/AndroidRuntime(663): FATAL EXCEPTION: main
07-27 12:00:33.431: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myexample.fragmentdemoexample/com.myexample.fragmentsupportexample.MainFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.os.Looper.loop(Looper.java:137)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 12:00:33.431: E/AndroidRuntime(663):  at java.lang.reflect.Method.invokeNative(Native Method)
07-27 12:00:33.431: E/AndroidRuntime(663):  at java.lang.reflect.Method.invoke(Method.java:511)
07-27 12:00:33.431: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 12:00:33.431: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 12:00:33.431: E/AndroidRuntime(663):  at dalvik.system.NativeStart.main(Native Method)
07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-27 12:00:33.431: E/AndroidRuntime(663):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.Activity.setContentView(Activity.java:1835)
07-27 12:00:33.431: E/AndroidRuntime(663):  at com.myexample.fragmentsupportexample.MainFragmentActivity.onCreate(MainFragmentActivity.java:14)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.Activity.performCreate(Activity.java:4465)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-27 12:00:33.431: E/AndroidRuntime(663):  ... 11 more
07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: java.lang.ClassCastException: com.myexample.fragmentdemoexample.MyListFragment cannot be cast to android.support.v4.app.Fragment
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
07-27 12:00:33.431: E/AndroidRuntime(663):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-27 12:00:33.431: E/AndroidRuntime(663):  ... 21 more

编辑这是我的完整代码 //MainActivity

public class MainFragmentActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_fragment, menu);
        return true;
    }

}

//详细活动

public class DetailActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Need to check if Activity has been switched to landscape mode
        // If yes, finished and go back to the s tart Activity
        if (getResources().getConfiguration().orientation == 
                Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }

        setContentView(R.layout.details_activity_layout);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String s = extras.getString("value");
            TextView view = (TextView) findViewById(R.id.detailsText);
            view.setText(s);
        }
    }
} 

//MyListFragment

public class MyListFragment extends ListFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux",
                "OS/2" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        DetailFragment fragment = (DetailFragment) (getActivity().getSupportFragmentManager()).findFragmentById(R.id.detailFragment);
        if (fragment != null && fragment.isInLayout()) {
            fragment.setText(item);
        } else {
            Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
            intent.putExtra("value", item);
            startActivity(intent);

        }

    }
}

//DetailsFragment

public class DetailFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.details, container, false);
        return view;
    }

    public void setText(String item) {
        TextView view = (TextView) getView().findViewById(R.id.detailsText);
        view.setText(item);
    }
}

//布局是 //activity_main.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="horizontal" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        class="com.myexample.fragmentdemoexample.MyListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.myexample.fragmentdemoexample.DetailFragment" >

        <!-- Preview: layout=@layout/details -->
    </fragment>

</LinearLayout>

//详细信息.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" >

    <TextView
        android:id="@+id/detailsText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dip"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

</LinearLayout> 

并且 layout-port 的代码类似于 Vogella 的帖子中提到的,并进行了适当的更改

4

2 回答 2

1

我就是这样做的:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; 
import android.util.Log;
import android.widget.Button;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class MainPoP extends SherlockFragmentActivity implements ListFragment.OnMainMenuListener{
    Button home;
    Button about;
    Button donate;
    Button explore;
    ActionBar actionbar;
    ListFragment mainMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        mainMenu = new ListFragment();
        ft.add(R.id.mainView, mainMenu).addToBackStack(null).commit();
    }
}

这就是我制作片段的方式,它工作得很好。尝试使用 OnCreateView 方法在扩展 Fragment 的类中像这样膨胀您的片段,或者是您要设置某些布局的特定片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.about_fragment, container, false);
    return v;
}
于 2012-07-27T20:33:04.230 回答
1

您所有的代码看起来都不错,请记住,您有两个 xml 文件 activity_main.xml 一个将在布局文件夹中,第二个将在 layout-port 文件夹中。

有关 Vogella 的完整工作演示,请查看此链接项目。

http://www.sendspace.com/file/vdhx6w

于 2012-07-30T06:42:46.533 回答