Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有动态片段的Android项目在emulator上崩溃?_Java_Android_Android Emulator_Android Fragments - Fatal编程技术网

Java 具有动态片段的Android项目在emulator上崩溃?

Java 具有动态片段的Android项目在emulator上崩溃?,java,android,android-emulator,android-fragments,Java,Android,Android Emulator,Android Fragments,我是android新手,在网上浏览各种教程来学习这些概念 最近我遇到了一些片段,并尝试实现它 现在我正在尝试实现如何将一个片段动态添加到一个活动中,但是当我尝试在emulator上启动我的应用程序时,我得到了一个崩溃报告(emulator不会崩溃) 这是我的密码 Main Activity.java public class MainActivity extends Activity { Fragment fragment; Button btn1, btn2, btn3;

我是android新手,在网上浏览各种教程来学习这些概念

最近我遇到了一些片段,并尝试实现它

现在我正在尝试实现如何将一个片段动态添加到一个活动中,但是当我尝试在emulator上启动我的应用程序时,我得到了一个崩溃报告(emulator不会崩溃)

这是我的密码

Main Activity.java

public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);
        
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_2, fragment);
        transaction.commit();
        
        btn1.setOnClickListener(btnClickListener);
    }

    @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;
    }   
    
    OnClickListener btnClickListener = new OnClickListener(){
        Fragment fragment;
        @SuppressLint("Recycle")
        @Override
        public void onClick(View v) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            if(v == btn1){
                fragment = new Fragment1();
            }
            transaction.replace(R.id.fragment_1,fragment);
            transaction.addToBackStack(null);
            transaction.commit();           
        }
        
    };
}
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:background="#333300">
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:id="@+id/btn1"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/container"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>
fragment1.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" 
    android:id="@+id/fragment_1"
    android:background="#FFDDAA">
    

</LinearLayout>

首先,在使用
Fragment


然后,如果它再次强制关闭,则发布logcat错误代码

首先,在使用
片段
时,必须使用
片段活动


然后,如果它再次强制关闭,则显示logcat错误代码

,您应该对布局进行充气

将以下内容视为片段_1.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" 
    android:id="@+id/fragment_1"
    android:background="#FFDDAA">


</LinearLayout>


在您的
main活动中

改变

  transaction.add(R.id.fragment_2, fragment);

bcoz您正在将片段添加到容器中,该容器在您的案例中是一个
线性布局

<LinearLayout
    android:id="@+id/container" // id is container not fragment_1
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>

你应该扩大布局

将以下内容视为片段_1.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" 
    android:id="@+id/fragment_1"
    android:background="#FFDDAA">


</LinearLayout>


在您的
main活动中

改变

  transaction.add(R.id.fragment_2, fragment);

bcoz您正在将片段添加到容器中,该容器在您的案例中是一个
线性布局

<LinearLayout
    android:id="@+id/container" // id is container not fragment_1
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>

在您的
main活动中
更改如下:

而不是id
transaction.add(R.id.fragment_2,fragment)将其更改为
transaction.add(R.id.container,fragment)

同样,单击按钮也可以

public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment);
        transaction.commit();

    }

在您的
main活动中
更改如下:

而不是id
transaction.add(R.id.fragment_2,fragment)将其更改为
transaction.add(R.id.container,fragment)

同样,单击按钮也可以

public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment);
        transaction.commit();

    }

在MainActivity的onCreate方法中,您试图从
MainActivity
加载
片段1
的位置。您的Logcat在哪里?您必须在此行
transaction.add(R.id.fragment_2,fragment)中获得错误因为它无法在您的布局中获取视图
R.id.fragment_2
。您的问题是将
片段
加载到未识别的布局中,该布局是
R.id.fragment_2
,它根本不在您的mainactivity布局中。如果您试图在mainactivitie的
线性布局中加载片段
,请在
R.id.fragment\u 2
之外定义其id。fragment\u 2类似于fragment\u 1。我不想用类似的信息来掩盖sapce,这就是为什么我没有添加它。但是我的项目中有一个Fragment_u2类。您试图从您的
MainActivity
中的
Fragment 1
加载到哪里?在MainActivity的onCreate方法中。您的日志猫在哪里?您必须在此行
transaction.add(R.id.fragment_2,fragment)中获得错误因为它无法在您的布局中获取视图
R.id.fragment_2
。您的问题是将
片段
加载到未识别的布局中,该布局是
R.id.fragment_2
,它根本不在您的mainactivity布局中。如果您试图在mainactivitie的
线性布局中加载片段
,请在
R.id.fragment\u 2
之外定义其id。fragment\u 2类似于fragment\u 1。我不想用类似的信息来掩盖sapce,这就是为什么我没有添加它。但我的项目中有一个Fragment_u2类。嗨,Naresh,当我使用支持库来使用fragmentManager和事务时,我试过这样做。我想您在检查后错放了xml文件。一次性交换活动xml文件和片段xmlcheck@NARESHREDDY无需扩展
FragmentActivity
始终取决于api级别。您好,Naresh,我在使用支持库使用fragmentManager和事务时尝试过这一点。我认为您在检查后错放了xml文件。一次性交换活动xml文件和片段xmlcheck@NARESHREDDY无需扩展碎片活动始终取决于api级别。
<LinearLayout
    android:id="@+id/container" // id is container not fragment_1
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>
  fragment = new Fragment2(); // Framgent2
  transaction.replace(R.id.container,fragment); 
  // replace Fragment1 by Fragment 2 in the container
  transaction.addToBackStack(null);
public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment);
        transaction.commit();

    }