扩展片段xml时出错

时间:2016-02-15 04:30:10

标签: java android android-fragments textview fatal-error

我正在尝试根据传递的意图更新片段中的textview,但我一直收到错误。这是我的代码:

public class PlayerDisplayFragment extends Fragment {

public PlayerDisplayFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_player_display, container, false);

    String[] playerArray = getArguments().getStringArray("playerArray");
    updateText(view,playerArray);

    return view;
}

public void updateText (View view , String[] playerArray){

    String pName = playerArray[0] + " " + playerArray[1];
    TextView playerName = (TextView) view.findViewById(R.id.player_name);
    playerName.setText(pName);

}
}

代码在尝试执行updateText函数时崩溃。这是它引用的xml元素(在片段xml文件中):

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Placeholder"
    android:textSize="18sp"
    android:id="@+id/player_name"
    android:gravity="center">
</TextView>

最后,这是错误:

26609-26609/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.soccerstar, PID: 26609
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.soccerstar/com.example.android.soccerstar.PlayerDisplay}:
                           android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
   at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
   at android.app.Activity.setContentView(Activity.java:2172)
   at com.example.android.soccerstar.PlayerDisplay.onCreate(PlayerDisplay.java:13)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

编辑:完整的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"
tools:context="com.example.android.soccerstar.PlayerDisplayFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/placeholder_m"
            android:background="#7b7b7b"
            android:layout_alignParentTop="true"
            android:id="@+id/PlayerPhoto"/>
        />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:gravity="center"
            android:textSize="16sp"
            android:textColor="#000000"
            android:text="You matched Lionel Messi!"
            android:layout_below="@+id/PlayerPhoto"
            android:id="@+id/MatchText">
            </TextView>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:layout_below="@+id/MatchText"
            >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Your Name"
                        android:textSize="18sp"
                        android:gravity="center">
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:layout_margin="8dp"
                        android:text="Born on April 15, 1992"
                        android:textSize="14sp"
                        android:gravity="center">
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:layout_margin="8dp"
                        android:text="Is 5 foot 11 inches tall."
                        android:textSize="14sp"
                        android:gravity="center">
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:layout_margin="8dp"
                        android:text="Weighs 160 lbs."
                        android:textSize="14sp"
                        android:gravity="center">
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:layout_margin="8dp"
                        android:text="Prefers right foot"
                        android:textSize="14sp"
                        android:gravity="center">
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="8dp"
                        android:layout_margin="8dp"
                        android:text="American Nationality"
                        android:textSize="14sp"
                        android:gravity="center">
                    </TextView>

                </LinearLayout>
            <View
                android:layout_width="1dp"
                android:layout_height="300dp"
                android:background="@android:color/darker_gray"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Lionel Messi"
                    android:textSize="18sp"
                    android:id="@+id/player_name"
                    android:gravity="center">
                </TextView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:layout_margin="8dp"
                    android:text="Born on April 15, 1992"
                    android:textSize="14sp"
                    android:gravity="center">
                </TextView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:layout_margin="8dp"
                    android:text="Is 5 foot 11 inches tall."
                    android:textSize="14sp"
                    android:gravity="center">
                </TextView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:layout_margin="8dp"
                    android:text="Weighs 160 lbs."
                    android:textSize="14sp"
                    android:gravity="center">
                </TextView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:layout_margin="8dp"
                    android:text="Prefers right foot"
                    android:textSize="14sp"
                    android:gravity="center">
                </TextView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:layout_margin="8dp"
                    android:text="American Nationality"
                    android:textSize="14sp"
                    android:gravity="center">
                </TextView>

            </LinearLayout>

        </LinearLayout>
    </RelativeLayout>
</ScrollView>

以下是主要活动:

public class PlayerDisplay extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_display); <--- THIS IS LINE 13
}

这是activity_player_display.xml文件:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.soccerstar.PlayerDisplayFragment"
tools:layout="@layout/fragment_player_display"  android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayerDisplay" />

我从主片段输入数组:

            Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class);
            intent.putExtra("playerArray", playerArray);
            startActivity(intent);

2 个答案:

答案 0 :(得分:0)

像这样使用主片段中的Bundle。

Fragment fragment = new PlayerFragment();
Bundle bundle = new Bundle();
bundle.putStringArray("playerArray", playerArray);
fragment.setArguments(bundle);

在你的Player Fragment getArguments中。

Bundle bundle = this.getArguments();
if(bundle != null){
    String[] playerArray = getArguments().getStringArray("playerArray");
}

希望这会对你有所帮助。

答案 1 :(得分:0)

终于明白了。这很简单!

正在引用的第13行代码,它来自片段的主要活动。它引用了主要活动xml文件而不是片段xml文件。所以我改变了它:

公共类PlayerDisplay扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_display); <--- THIS IS LINE 13
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_player_display); <--- THIS IS LINE 13
}

现在它有效!谢谢大家。

相关问题