gpt4 book ai didi

java - 该类型的方法和构造函数未定义

转载 作者:行者123 更新时间:2023-12-02 05:37:42 25 4
gpt4 key购买 nike

我有这样的错误消息

The method setContentView(int, FindPeopleFragment) is undefined for the type FindPeopleFragment

The constructor BoxOfficeMoviesAdapter(FindPeopleFragment, ArrayList<BoxOfficeMovie>) is undefined

The constructor Intent(FindPeopleFragment, Class<BoxOfficeDetailActivity>) is undefined

那么,我的代码需要改进什么?这是我的代码,我想在 FindPeopleFragment 中显示 ListView Box Office

BoxOfficeMoviesAdapter.java

import info.androidhive.slidingmenu.R;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class BoxOfficeMoviesAdapter extends ArrayAdapter<BoxOfficeMovie> {
public BoxOfficeMoviesAdapter(Context context, ArrayList<BoxOfficeMovie> aMovies) {
super(context, 0, aMovies);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
BoxOfficeMovie movie = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_box_office_movie, null);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
TextView tvCriticsScore = (TextView) convertView.findViewById(R.id.tvCriticsScore);
TextView tvCast = (TextView) convertView.findViewById(R.id.tvCast);
ImageView ivPosterImage = (ImageView) convertView.findViewById(R.id.ivPosterImage);
// Populate the data into the template view using the data object
tvTitle.setText(movie.getTitle());
tvCriticsScore.setText("Score: " + movie.getCriticsScore() + "%");
tvCast.setText(movie.getCastList());
Picasso.with(getContext()).load(movie.getPosterUrl()).into(ivPosterImage);
// Return the completed view to render on screen
return convertView;
}
}

BoxOfficeMovieDetail.java

import info.androidhive.slidingmenu.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class BoxOfficeDetailActivity extends Activity {
private ImageView ivPosterImage;
private TextView tvTitle;
private TextView tvSynopsis;
private TextView tvCast;
private TextView tvAudienceScore;
private TextView tvCriticsScore;
private TextView tvCriticsConsensus;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_office_detail);
// Fetch views
ivPosterImage = (ImageView) findViewById(R.id.ivPosterImage);
tvTitle = (TextView) findViewById(R.id.tvTitle);
tvSynopsis = (TextView) findViewById(R.id.tvSynopsis);
tvCast = (TextView) findViewById(R.id.tvCast);
tvCriticsConsensus = (TextView) findViewById(R.id.tvCriticsConsensus);
tvAudienceScore = (TextView) findViewById(R.id.tvAudienceScore);
tvCriticsScore = (TextView) findViewById(R.id.tvCriticsScore);
// Load movie data
BoxOfficeMovie movie = (BoxOfficeMovie) getIntent().getSerializableExtra(BoxOfficeActivity.MOVIE_DETAIL_KEY);
loadMovie(movie);
}

// Populate the data for the movie
@SuppressLint("NewApi")
public void loadMovie(BoxOfficeMovie movie) {
if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setTitle(movie.getTitle());
}
// Populate data
tvTitle.setText(movie.getTitle());
tvCriticsScore.setText(Html.fromHtml("<b>Critics Score:</b> " + movie.getCriticsScore() + "%"));
tvAudienceScore.setText(Html.fromHtml("<b>Audience Score:</b> " + movie.getAudienceScore() + "%"));
tvCast.setText(movie.getCastList());
tvSynopsis.setText(Html.fromHtml("<b>Synopsis:</b> " + movie.getSynopsis()));
tvCriticsConsensus.setText(Html.fromHtml("<b>Consensus:</b> " + movie.getCriticsConsensus()));
// R.drawable.large_movie_poster from
// http://content8.flixster.com/movie/11/15/86/11158674_pro.jpg -->
Picasso.with(this).load(movie.getLargePosterUrl()).
placeholder(R.drawable.large_movie_poster).
into(ivPosterImage);
}

}

FindPeopleFragment.java

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.JsonHttpResponseHandler;

import info.androidhive.slidingmenu.boxoffice.BoxOfficeActivity;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeDetailActivity;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeMovie;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeMoviesAdapter;
import info.androidhive.slidingmenu.boxoffice.RottenTomatoesClient;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class FindPeopleFragment extends Fragment {

private ListView lvMovies;
private BoxOfficeMoviesAdapter adapterMovies;
private RottenTomatoesClient client;
public static final String MOVIE_DETAIL_KEY = "movie";

public FindPeopleFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_office, this);
lvMovies = (ListView) getView().findViewById(R.id.lvMovies);
ArrayList<BoxOfficeMovie> aMovies = new ArrayList<BoxOfficeMovie>();
adapterMovies = new BoxOfficeMoviesAdapter(this, aMovies);
lvMovies.setAdapter(adapterMovies);
// Fetch the data remotely
fetchBoxOfficeMovies();
setupMovieSelectedListener();
}

private void fetchBoxOfficeMovies() {
client = new RottenTomatoesClient();
client.getBoxOfficeMovies(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int code, JSONObject body) {
JSONArray items = null;
try {
items = body.getJSONArray("movies");

ArrayList<BoxOfficeMovie> movies = BoxOfficeMovie.fromJson(items);

adapterMovies.addAll(movies);
adapterMovies.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});

}

public void setupMovieSelectedListener() {
lvMovies.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View item, int position, long rowId) {
Intent i = new Intent(FindPeopleFragment.this, BoxOfficeDetailActivity.class);
i.putExtra(MOVIE_DETAIL_KEY, adapterMovies.getItem(position));
startActivity(i);
}
});
}}

请帮我解决一下,谢谢

最佳答案

The constructor BoxOfficeMoviesAdapter(FindPeopleFragment, ArrayList) is undefined

在 Fragment 中,将上下文传递给构造函数时,使用 getActivity() 而不是 this

改变这个

adapterMovies = new BoxOfficeMoviesAdapter(this, aMovies);

adapterMovies = new BoxOfficeMoviesAdapter(getActivity(), aMovies);

The method setContentView(int, FindPeopleFragment) is undefined for the type FindPeopleFragment

setContentView(R.layout.activity_box_office, this);

必须删除

setContentview 是 Activity 类的方法,而不是 Fragment

更改为

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_box_office,container,false);
// rest of the code
return view;

}

进一步getView()返回null

改变这个

lvMovies = (ListView) getView().findViewById(R.id.lvMovies);

lvMovies = (ListView) view.findViewById(R.id.lvMovies);

The constructor Intent(FindPeopleFragment, Class) is undefined

最后改变这个

Intent i = new Intent(FindPeopleFragment.this, BoxOfficeDetailActivity.class);

Intent i = new Intent(getActivity(), BoxOfficeDetailActivity.class); 

在继续之前,您最好先阅读 Fragment 文档。

关于java - 该类型的方法和构造函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817022/

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