2016-05-27 28 views
0

閱讀「首先Android編程」,它有點過時,所以本書中顯示的代碼也過時了。 My Code錯誤的第二個參數類型與片段

Android Studio說:錯誤的第二個參數類型。 發現:com.xfunny.workout.WorkoutDetailFragment '要求:android.app.Fragment'

public class WorkoutDetailFragment extends Fragment { 

private long workoutId; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_workout_detail, container, false); 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    View view = getView(); 
    if(view != null){ 
     TextView title = (TextView) view.findViewById(R.id.textTitle); 
     Workout workout = Workout.workouts[(int)workoutId]; 
     title.setText(workout.getName()); 
     TextView description = (TextView) view.findViewById(R.id.textDescription); 
     description.setText(workout.getDescription()); 
    } 
} 

public void setWorkout(long id){ 
    this.workoutId = id; 
} 

}

+1

請張貼代碼(不是圖片)的'WorkoutDetailFragment' – tyczj

+0

不'WorkoutDetailFragment'擴展'Fragment'? –

+0

是的,它擴展了片段。 –

回答

0

做財產以後這樣

android.app.Fragment details = new WorkoutDetailFragment(); 

代替

WorkoutDetailFragment details = new WorkoutDetailFragment(); 
+0

謝謝,但它不起作用。它使用'android.app.Fragment'代替'WorkoutDetailFragment'時表示「不兼容的類型」。 –

0

由於您使用的是AppCompatActivity,因此您需要使用Fragment從支持庫,即import android.support.v4.app.Fragment

當然,如果你切換到支持片段,您將需要使用getSupportFragmentManager(),而不是getFragmentManager()

+0

謝謝,但它不起作用。當我使用'getSupportFragmentManager()'而不是'getFragmentManager()'時,它說「不兼容的類型」。 –

+0

您必須更改WorkoutDetailFragment類中片段的導入語句 –

+0

並且請更改FragmentManager的Import語句以及 –

相關問題