有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有这样的错误信息

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票房

BoxOfficeMovieAdapter。爪哇

import info.安卓hive.slidingmenu.R;

import java.util.ArrayList;

import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ImageView;
import 安卓.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。爪哇

import info.安卓hive.slidingmenu.R;
import 安卓.annotation.SuppressLint;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.text.Html;
import 安卓.widget.ImageView;
import 安卓.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 (安卓.os.Build.VERSION.SDK_INT>=安卓.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);
    }

}

找到人的碎片。爪哇

import java.util.ArrayList;

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

import com.loopj.安卓.http.JsonHttpResponseHandler;

import info.安卓hive.slidingmenu.boxoffice.BoxOfficeActivity;
import info.安卓hive.slidingmenu.boxoffice.BoxOfficeDetailActivity;
import info.安卓hive.slidingmenu.boxoffice.BoxOfficeMovie;
import info.安卓hive.slidingmenu.boxoffice.BoxOfficeMoviesAdapter;
import info.安卓hive.slidingmenu.boxoffice.RottenTomatoesClient;
import 安卓.app.Fragment;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemClickListener;
import 安卓.widget.Button;
import 安卓.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);
            }
        });
    }}

请帮我修一下,谢谢


共 (1) 个答案

  1. # 1 楼答案

    The constructor BoxOfficeMoviesAdapter(FindPeopleFragment, ArrayList) is undefined

    在片段中,将上下文传递给构造函数时,使用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是活动类的方法,而不是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); 
    

    在继续之前,您最好先阅读片段文档