gpt4 book ai didi

java - 您能指导我代码中有什么问题吗?

转载 作者:行者123 更新时间:2023-12-01 09:51:44 25 4
gpt4 key购买 nike

MainActivity.java 中转换 WorkoutDetailFragment 的行显示无法完成的错误。请告诉我为什么它不起作用。我从书上复制了代码。

主要 Activity

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);
}
}

锻炼.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);
}

}

最佳答案

使用getSupportFragmentManager()而不是getFragmentManager()

关于java - 您能指导我代码中有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com