3

我真的卡在這個錯誤(如下圖):無法解析方法placeholderfragment錯誤

 @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_vehiclerecall); 

     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
} 

我有正確引用到XML佈局(下圖)的容器:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    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:id="@+id/container" 
    tools:context="com.customerautomotivenetwork.VehicleRecall"/> 

,但我一直無法解決PlacehlderFragment方法的錯誤,對我在做什麼錯誤的任何想法?

+2

你可以發佈堆棧跟蹤嗎? – 2014-09-19 13:27:23

+0

這是我得到使用堆棧跟蹤:<?XML版本= 「1.0」 編碼= 「UTF-8」> <的FrameLayout 的xmlns:機器人=「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:id =」@ + id/container「 tools:忽略=「MergeRootFrame」 工具:上下文=「com.customerautomotivenetwork.VehicleRecall」 /> – 2014-09-19 14:51:28

+0

堆棧跟蹤基本上是錯誤消息時應用崩潰,而不是XML佈局。它顯示了線程終止之前堆棧上所有方法調用的蹤跡。你可以在Eclipse的logcat窗口中找到它。 – 2014-09-19 15:12:43

回答

3
  • 你可能延長Fragment

錯誤的版本在文件PlaceholderFragment.java,首先要確保你延長Fragment

public class PlaceholderFragment extends Fragment { //... 

然後,在該文件的開頭檢查進口。應該有一行:

import android.app.Fragment; 

行:

import android.support.v4.app.Fragment; 

儘量不要混用「正常」和「V4」片段,它們具有相同的名字,做同樣的事情但他們實際上是不同的課程。

1

我只好導入android.support.v4.app.Fragment來解決這個問題。

在我來說,我使用的minSdkVersion 8,我創造了一個Activity類的java>包裝>胡亞蓉> BlankActivity。這個創建的活動類有一個內部類public static class PlaceholderFragment extends Fragment。這個內部類用於onCreate()中

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
}