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);

        }

    }
}

//詳細フラグメント

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>

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

また、layout-port のコードは、適切な変更を加えた Vogella の投稿に記載されているものと似ています

4

2 に答える 2

1

すべてのコードは見栄えがよくなりますが、2 つの xml ファイル activity_main.xml が 1 つがレイアウト フォルダーにあり、2 つ目がレイアウト ポート フォルダーにあることに注意してください。

Vogella の完全な動作デモについては、このリンク プロジェクトをチェックしてください。

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

于 2012-07-30T06:42:46.533 に答える
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 に答える