我从上一个问题中得到了更好的表述
单选按钮
点击RadioButton1
.. Activity1
应显示在same screen
再次点击RadioButton1
Activity2
应显示在same screen
类似于RadioButton2
我已经使用tabhost
执行了标签由于tabhost的设计很僵硬.....可以如上所述进行
如果是这样,怎么办呢?
我不知道如何使用radiobutton,但我尝试了类似的东西
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_main, menu);
return true;
}
}
Fragment_1.java
public class Fragment_1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);
final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName);
Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi);
btnSayHi_Fragment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = edtxtPersonName_Fragment.getText().toString();
FragmentManager fm = getFragmentManager();
Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2);
if(f2 != null && f2.isInLayout())
{
f2.setName(name);
}
Activity activity = getActivity();
if(activity != null)
{
Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
Fragment_2.java
public class Fragment_2 extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment_fragment_2, container, false);
return view;
}
public void setName(String name)
{
TextView txtName = (TextView) view.findViewById(R.id.txtViewResult);
txtName.setText("Hi " + name);
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment_content_1"
android:name="com.example.fragmentexample.Fragment_1"
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="fill_parent" >
</fragment>
<fragment
android:id="@+id/fragment_content_2"
android:name="com.example.fragmentexample.Fragment_2"
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="fill_parent" >
<!-- Preview: layout=@layout/fragment_basic -->
</fragment>
</LinearLayout>
我仍然遇到了日志错误
日志::
10-13 15:56:44.837: D/AndroidRuntime(326): Shutting down VM
10-13 15:56:44.837: W/dalvikvm(326): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-13 15:56:45.117: E/AndroidRuntime(326): FATAL EXCEPTION: main
10-13 15:56:45.117: E/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment/com.example.fragment.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.os.Looper.loop(Looper.java:123)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 15:56:45.117: E/AndroidRuntime(326): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 15:56:45.117: E/AndroidRuntime(326): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 15:56:45.117: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 15:56:45.117: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 15:56:45.117: E/AndroidRuntime(326): at dalvik.system.NativeStart.main(Native Method)
10-13 15:56:45.117: E/AndroidRuntime(326): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-13 15:56:45.117: E/AndroidRuntime(326): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.Activity.setContentView(Activity.java:1657)
10-13 15:56:45.117: E/AndroidRuntime(326): at com.example.fragment.MainActivity.onCreate(MainActivity.java:16)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 15:56:45.117: E/AndroidRuntime(326): ... 11 more
10-13 15:56:45.117: E/AndroidRuntime(326): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.fragment-1.apk]
10-13 15:56:45.117: E/AndroidRuntime(326): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-13 15:56:45.117: E/AndroidRuntime(326): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-13 15:56:45.117: E/AndroidRuntime(326): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
10-13 15:56:45.117: E/AndroidRuntime(326): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-13 15:56:45.117: E/AndroidRuntime(326): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
10-13 15:56:45.117: E/AndroidRuntime(326): ... 20 more
10-13 15:56:49.207: I/Process(326): Sending signal. PID: 326 SIG: 9
答案 0 :(得分:0)
请勿使用RadioButtons
,因此您应使用TabHost
。
请在此处查看答案: https://stackoverflow.com/a/17236958/819355
另一个不错的选择是使用ViewPager
,请参阅此处的官方文档:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html