Android Fragment Transaction提交Crashes App

时间:2014-05-22 05:44:25

标签: android android-fragments

处理我的第一个Android Fragment Project,并遇到我的应用程序在启动时崩溃的问题。下面是我的Fra

代码
package com.example.fragment;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {


SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    StartFragment startFragment = new StartFragment();
    transaction.add(R.id.start_fragment, startFragment);

    transaction.commitAllowingStateLoss();


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new Fragment();
        if(position == 0)
        {
        fragment = new Frag1();

        }
        if(position == 1)
        {
        fragment = new Frag2();

        }


        return fragment;
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    private final String[] titles = { "Frag1", "Frag2"};

    //...

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}


   }

Activity_main.xml文件:

  <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >


<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" />

   </android.support.v4.view.ViewPager>

logcat的:

05-22 00:37:30.892: E/AndroidRuntime(20472):at       android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at java.lang.reflect.Method.invoke(Method.java:515)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at dalvik.system.NativeStart.main(Native Method)
05-22 00:37:30.892: E/AndroidRuntime(20472): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080002 (com.example.andy_xmark1:id/start_fragment) for fragment StartFragment{426c8c98 #0 id=0x7f080002}
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.app.Activity.performStart(Activity.java:5241)
05-22 00:37:30.892: E/AndroidRuntime(20472):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-22 00:37:30.892: E/AndroidRuntime(20472):    ... 11 more

1 个答案:

答案 0 :(得分:3)

在行中:

transaction.add(R.id.start_fragment, startFragment);

第一个参数需要是容器布局的id,它将是片段的位置。

xml中没有R.id.start_fragment id的布局。

此外,您的xml根元素是ViewPager,您不能以这种方式添加片段(使用片段事务,而是需要使用适配器)。

首先,尝试用以下内容替换所有xml内容:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/R.id.start_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</FrameLayout>