我正处于片段的学习阶段,所以如果我的问题听起来很愚蠢,那么请放手让它给你建议。
所以我从Click here开发了一个非常简单的应用程序,并在我的其他类文件中发布了一些错误。
接口
package com.example.fragmentdemo;
public interface Communicator {
public void respond(String data);
}
活动
package com.example.fragmentdemo;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity implements Communicator{
@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.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void respond(String data) {
// TODO Auto-generated method stub
FragmentManager manager=getFragmentManager();
FragmentB f2=(FragmentB) manager.findFragmentById(R.id.fragment_b);
f2.changeText(data);
}
}
FragmentA
package com.example.fragmentdemo;
import android.app.Fragment;
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 FragmentA extends Fragment{
Button btn;
int counter=0;
Communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(savedInstanceState==null)
{
counter=0;
}
else
{
counter=savedInstanceState.getInt("counter");
}
return inflater.inflate(R.id.fragment_a,container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btn=(Button)getActivity().findViewById(R.id.button1);
comm=(Communicator)getActivity();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
comm.respond(counter+""+" times");
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("counter",counter);
}
}
FragmentB
package com.example.fragmentdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentB extends Fragment {
TextView txt;
String data;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(savedInstanceState==null)
{
}
else
{
data=savedInstanceState.getString("data");
}
return inflater.inflate(R.id.fragment_b, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
txt = (TextView) getActivity().findViewById(R.id.textView2);
}
public void changeText(String dta)
{
txt.setText(dta);
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("data", data);
}
}
activity.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragmentdemo.MainActivity"
android:id="@+id/my_lay">
<fragment
android:id="@+id/fragment_b"
android:name="com.example.fragmentdemo.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fragment_a"
android:layout_marginTop="36dp"
android:layout_toLeftOf="@+id/fragment1" />
<fragment
android:id="@+id/fragment_a"
android:name="com.example.fragmentdemo.FragmentA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
fragment_a
<?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:background="#FF0000" >
<Button
android:layout_margin="20dp"
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
fragment_b
<?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:background="#00FF00" >
<TextView
android:layout_margin="20dp"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
记录错误
07-22 02:43:13.076: E/AndroidRuntime(1784): FATAL EXCEPTION: main
07-22 02:43:13.076: E/AndroidRuntime(1784): Process: com.example.fragmentdemo, PID: 1784
07-22 02:43:13.076: E/AndroidRuntime(1784): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentdemo/com.example.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.os.Looper.loop(Looper.java:136)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-22 02:43:13.076: E/AndroidRuntime(1784): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 02:43:13.076: E/AndroidRuntime(1784): at java.lang.reflect.Method.invoke(Method.java:515)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-22 02:43:13.076: E/AndroidRuntime(1784): at dalvik.system.NativeStart.main(Native Method)
07-22 02:43:13.076: E/AndroidRuntime(1784): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.setContentView(Activity.java:1929)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.MainActivity.onCreate(MainActivity.java:17)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.performCreate(Activity.java:5231)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-22 02:43:13.076: E/AndroidRuntime(1784): ... 11 more
07-22 02:43:13.076: E/AndroidRuntime(1784): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090051 type #0x12 is not valid
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2314)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.content.res.Resources.getLayout(Resources.java:939)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.FragmentB.onCreateView(FragmentB.java:25)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Fragment.performCreateView(Fragment.java:1700)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.onCreateView(Activity.java:4786)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.MainActivity.onCreateView(MainActivity.java:1)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-22 02:43:13.076: E/AndroidRuntime(1784): ... 21 more
答案 0 :(得分:2)
在这两个片段中,你正在夸大'id':
return inflater.inflate(R.id.fragment_a,container, false);
return inflater.inflate(R.id.fragment_b,container, false);
但你需要调整布局:
return inflater.inflate(R.layout.fragment_a,container, false);
return inflater.inflate(R.layout.fragment_b,container, false);
答案 1 :(得分:2)
在每个
中的片段类中初始化它View rootView;
创建
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView= inflater.inflate(R.layout.fragment_a, container, false);
btn=(Button)rootView.findViewById(R.id.button1);
// do for all like that
return rootView;
}
答案 2 :(得分:1)
布局不是ID,请在两个片段的onCreateView()中使用以下内容。你需要夸大布局
return inflater.inflate(R.layout.fragment_a,container, false);
return inflater.inflate(R.layout.fragment_b,container, false);