gpt4 book ai didi

android fragment 无法在模拟器中打开

转载 作者:行者123 更新时间:2023-11-30 02:08:54 25 4
gpt4 key购买 nike

首先,我是新的 android 和英语 :) 对此感到抱歉。我对 fragment 管理器有疑问。我阅读了一些在主要 Activity 中使用 fragment 的代码。但是它不能在模拟器中运行,您也可以在下面查看代码。我认为关于“import android.support.v4.app.FragmentManager;”的问题线。我不知道也许我必须像那样使用它“import android.app.FragmenManager”我希望你能理解我的句子并帮助我。谢谢。

   package com.example.ilk.fragmenttransactions;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;



public class MainActivity extends FragmentActivity {

FragmentManager fm;

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

public void addA(View v){
FragmentA f1 = new FragmentA();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f1, "A");
transaction.commit();
}

public void removeA(View v){
FragmentA f1 = (FragmentA)fm.findFragmentByTag("A");
FragmentTransaction transaction = fm.beginTransaction();
if(f1 != null){
transaction.remove(f1);
transaction.commit();
}
else{
Toast.makeText(this, "There is no A! you cannot delete it", Toast.LENGTH_LONG).show();
}
}

public void addB(View v){
FragmentB f2 = new FragmentB();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f2, "B");
transaction.commit();
}

public void removeB(View v){
FragmentB f2 = (FragmentB)fm.findFragmentByTag("B");
FragmentTransaction transaction = fm.beginTransaction();
if(f2 != null){
transaction.remove(f2);
transaction.commit();
}
else{
Toast.makeText(this, " There is no B! you cannot delete it ",Toast.LENGTH_SHORT).show();
}
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

使用import android.support.v4.app.FragmentManager

fragment 未加载,因为您在应用初始化时未进行任何交易:

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

我认为您正在尝试通过单击布局的元素来加载 fragment

android:onClick 属性

例如:

<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addA" />

执行这个方法的:

  public void addA(View v){
FragmentA f1 = new FragmentA();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f1, "A");
transaction.commit();
}

关于android fragment 无法在模拟器中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384378/

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