gpt4 book ai didi

java - 在OnCreate中执行 fragment 转换时出错, "No view found"

转载 作者:行者123 更新时间:2023-12-01 13:28:35 26 4
gpt4 key购买 nike

错误:

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

主类:

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>

好吧,伙计们,我在尝试将 fragment 放入 onCreate() 中的框架时遇到错误,我的应用程序有一个名为“fragment_home”的选项卡的 Split View,该选项卡有 2 个按钮,一个用于 ir 的 ListView ,一个用于 ir 的 ListView 其他用于它的 map View ,当单击此按钮时, fragment 会出现在位于按钮顶部的框架上............ ........................

最佳答案

而不是使用替换 fragment ,使用添加 fragment 和删除 fragment ..

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

关于java - 在OnCreate中执行 fragment 转换时出错, "No view found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21682072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com