Java 具有动态片段的Android项目在emulator上崩溃?
我是android新手,在网上浏览各种教程来学习这些概念 最近我遇到了一些片段,并尝试实现它 现在我正在尝试实现如何将一个片段动态添加到一个活动中,但是当我尝试在emulator上启动我的应用程序时,我得到了一个崩溃报告(emulator不会崩溃) 这是我的密码 Main Activity.javaJava 具有动态片段的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;
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活动中
更改如下:
而不是idtransaction.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活动中
更改如下:
而不是idtransaction.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();
}