在OnCreate中执行片段转换时出错,“找不到视图”

时间:2014-02-10 16:06:06

标签: java android runtime-error

错误:

02-10 15:46:58.685: E/AndroidRuntime(1621): FATAL EXCEPTION: main
02-10 15:46:58.685: E/AndroidRuntime(1621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pap.myspots/com.pap.myspots.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05000e (com.pap.myspots:id/frame) for fragment MapFragment{52742240 #0 id=0x7f05000e}
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.os.Looper.loop(Looper.java:137)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at java.lang.reflect.Method.invoke(Method.java:525)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at dalvik.system.NativeStart.main(Native Method)
02-10 15:46:58.685: E/AndroidRuntime(1621): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05000e (com.pap.myspots:id/frame) for fragment MapFragment{52742240 #0 id=0x7f05000e}
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.Activity.performStart(Activity.java:5143)
02-10 15:46:58.685: E/AndroidRuntime(1621):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
02-10 15:46:58.685: E/AndroidRuntime(1621):     ... 11 more

MainClass:

package com.pap.myspots;

import com.pap.myspots.adapter.TabsPagerAdapter;
import com.pap.myspots.fragments.MapFragment;
import com.pap.myspots.fragments.NotificationsFragment;
import com.pap.myspots.fragments.SpotListFragment;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    String local;
    Button mapButton;
    Button listButton;
    boolean mapClicked;
    // Tab titles

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mapButton = (Button)findViewById(R.id.mapButton);
        listButton = (Button)findViewById(R.id.listButton);

        // Adding Tabs

        ImageView mapTab = new ImageView(this);
        mapTab.setBackgroundColor(Color.parseColor("#ffbb33"));
        //mapTab.setImageResource(R.drawable.notificationsicon);
        actionBar.addTab(actionBar.newTab().setIcon(R.drawable.nicon)
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setIcon(R.drawable.micon)
                .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setIcon(R.drawable.sicon)
                .setTabListener(this));



        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }



            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        viewPager.setCurrentItem(1, false);

        MapFragment mapfragment = new MapFragment();
        getSupportFragmentManager().beginTransaction()
        .add(R.id.frame, mapfragment).commit();
        mapClicked= true;
    }

    public void clickMap(View v){
        if(mapClicked == false){
            MapFragment mapfragment = new MapFragment();
            getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame, mapfragment).commit();
            mapClicked = true;

        }
    }


    public void clickList(View v){
        SpotListFragment listfragment = new SpotListFragment();
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.frame, listfragment).commit();
        mapClicked = false;
    }



    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

activity_main.xml中

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

fragment_home.xml

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

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/mapButton"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/button_maplist"
                android:text="Map"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:onClick="clickMap"/>

            <Button 
                android:id="@+id/listButton"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="List"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:background="@drawable/button_maplist"
                android:onClick="clickList"/> 

    </LinearLayout>
</RelativeLayout>

好吧,伙计们在尝试将片段放入onCreate()中的一个帧时遇到错误,我的应用程序使用一个名为“fragment_home”的标签分割视图,该标签有2个按钮,用于列表视图的ir和另外,对于它的地图视图,当单击此按钮时,片段出现在按钮顶部的框架上....................... ..............

1 个答案:

答案 0 :(得分:1)

而不是使用Replace Fragment使用Add Fragment和Remove Fragment ..

我已经在我的一个应用程序中执行此操作。它正常工作。