我是 Android 新手,正在尝试自学。但是我在使用 Fragments 时遇到了困难。我正在创建一个简单的应用程序来学习片段。我认为这可能看起来很愚蠢,但我真的无法让它发挥作用。
我想要做的就是点击 Fragment_One 中的一个按钮(buttonSayHi),Fragment_One 应该被 Fragment_Two 替换。
我不确定何时调用片段代码以及我应该在哪里编写代码来调用第二个片段。我收到错误:无法启动 Activity 组件。
但是,如果我放弃按钮的侦听器并且片段显示在活动中,则代码可以正常工作。
我进行了大量研究,阅读了 developer.android.com 上的片段教程以及 Lars Vogella 的教程。我认为我的概念不清楚。
任何帮助将不胜感激。
以下是代码:
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayoutFragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextPersonName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonSayHi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Say Hi"
android:onClick="onButtonClicked"/>
</LinearLayout>
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I will say Hi!" />
</LinearLayout>
MainActivity.java
package com.example.fragmenttutorial;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity{
View view;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragmentOne = new FragmentOne();
fragmentTransaction.add(R.id.frameLayoutFragmentContainer, fragmentOne);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
protected void onButtonClicked()
{
if(view.getId() == R.id.buttonSayHi){
Fragment fragmentTwo = new FragmentTwo();
fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
}
FragmentOne.java
package com.example.fragmenttutorial;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment{
View view;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
// protected void onButtonClicked()
// {
// if(view.getId() == R.id.buttonSayHi){
// Fragment fragmentTwo = new FragmentTwo();
//
// fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
// fragmentTransaction.addToBackStack(null);
//
// fragmentTransaction.commit();
//
// }
//
// }
}
我已经注释掉了片段中的点击代码。我也尝试在片段中实现 onClickListener。
片段二.java
package com.example.fragmenttutorial;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
编辑:我已经从我的 XML 代码中删除了 android:onClick="onButtonClicked" 行。并编辑了以下文件,但它仍然不起作用。你们能给我提供一个没有 android:onClick="onButtonClicked" 行的工作示例吗?
MainActivity.java
package com.example.fragmenttutorial;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity{
View view;
Fragment fragmentOne;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentOne = new FragmentOne();
fragmentTransaction.add(R.id.frameLayoutFragmentContainer, fragmentOne);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
FragmentOne.java
package com.example.fragmenttutorial;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentOne extends Fragment implements OnClickListener{
View view;
Fragment fragmentTwo;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Button buttonSayHi = (Button) view.findViewById(R.id.buttonSayHi);
buttonSayHi.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
fragmentTwo = new FragmentTwo();
fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
感谢您提出宝贵的建议。