2016-02-15 178 views
0

我想基於傳遞的意圖更新片段中的textview,但我不斷收到錯誤。這裏是我的代碼:錯誤膨脹片段xml

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); 
+0

你能發佈完整的片段xml代碼嗎? – Jayanth

+1

如果'@ + id/player_name'在**片段** xml中,那麼爲什麼你要在'getActivity()的活動中找到它「。findViewById(R.id.player_name)'?這不是你如何在片段 –

+0

jayanth中獲得觀點 - 這是相當長的。你正在尋找什麼特別的東西? 板球 - 是的,我意識到我絕對做錯了什麼。你將如何解決問題/在片段中獲取視圖? – NBC

回答

0

使用捆綁在你的主要碎片這樣。

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

並在您的播放器片段getArguments。

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

希望這會對你有幫助。

+0

這沒有奏效。謝謝你嘗試! – NBC

+0

@NBC:什麼不工作? –

+0

我仍然得到相同的錯誤 – NBC

0

終於明白了。非常簡單!

那行第13行被引用的代碼,它來自片段的主要活動。它引用主活動xml文件而不是片段xml文件。所以我改成了:

公共類PlayerDisplay延伸活動{

@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 
} 

而現在它的作品!謝謝大家。