如何为片段和动作条的应用

如何为片段和动作条的应用

本文介绍了如何为片段和动作条的应用,该应用支持Android 2.2+设备的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我曾参观过这么多的类似链接这样即1, 遵循各种例子/链接即的并的主要是我按照这个职位从的的太多次,每次我得到相同的异常....可能是什么原因呢?

I have visited so many similar links like this i.e 1, 2 followed various examples / links i.e. this one and this one mainly I have followed this post from Vogella that too multiple times, Every time I get same exception.... What could be the reason ?

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

修改这里是我的全code
// MainActivity

EDIT Here is my full code//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;
    }

}

// DetailActivity

//DetailActivity

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

//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

//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

//Layouts are//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>

// details.xml

//details.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>

和code布局端口类似的Vogella的帖子用适当的修改

and code for layout-port is similar to as mentioned on Vogella's post with appropriate changes

推荐答案

您所有的code是好看,只是记住,你有两个xml文件activity_main.xml中之一将是在布局文件夹和第二会在布局端口的文件夹中。

All your code is look good , just keep in mind that you have two xml file activity_main.xml one will be in layout folder and second will be in layout-port folder.

有关Vogella的完整的工作演示看看这个链接项目。

For complete working demo of Vogella check out this link project.

这篇关于如何为片段和动作条的应用,该应用支持Android 2.2+设备的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:43