0
WorkoutDetailFragment在其中傳遞的MainActivity.java中的行顯示無法完成的錯誤。請告訴我爲什麼它不起作用。我從書中複製了代碼。你能指導我在代碼中出了什麼問題嗎?
MainActivity
package gangster.workout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag);
frag.setWorkout(1);
}
}
Workout.Java
package gangster.workout;
public class Workout {
private String name;
private String description;
public static final Workout[] workouts = {
new Workout("The Limb Loosener" , " Pta nhi kya ksgdrna hai isme") ,
new Workout("The Timb Loosener" , " Pta nhi kya asdfasasfasasdkrna hai isme") ,
new Workout("The Pimb Loosener" , " Pta nhi kyaasf krna hai isme") ,
new Workout("The Zimb Loosener" , " Pta nhi kyasfasa krna hai isme") ,
};
private Workout(String name , String description){
this.name = name;
this.description = description;
}
public String getDescription(){
return description;
}
public String getName(){
return name;
}
public String toString(){
return this.name;
}
}
WorkoutDetailFragment.java
package gangster.workout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class WorkoutDetailFragment extends Fragment {
private long workoutId;
public void setWorkout(long id) {
this.workoutId = id;
}
@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());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
}
多德,它的工作原理。謝謝。使用那個有什麼錯誤? –
你的代碼表明你使用'android.support.v7.app.AppCompatActivity'和'android.support.v4.app.Fragment',它們都來自android支持庫。這需要通過調用'getSupportFragmentManager()'獲得正確的'FragmentManager'。請接受答案,因爲它解決了你的問題:) –
我是一個初學者。你能不能更復雜地解釋一下? :P 4分鐘後我可以接受答案。 –