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

时间:2014-07-18 04:01:22

标签: java android android-fragments android-listview syntax-error

我有像这样的错误消息

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

请帮我解决,谢谢

1 个答案:

答案 0 :(得分:2)

  

构造函数BoxOfficeMoviesAdapter(FindPeopleFragment,   ArrayList)未定义

在将片段传递给构造函数时,在片段中使用getActivity()而不是this

更改此

adapterMovies = new BoxOfficeMoviesAdapter(this, aMovies);

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

方法setContentView(int,FindPeopleFragment)未定义   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);
  

构造函数Intent(FindPeopleFragment,   类)未定义

最后改变这个

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

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

在进一步阅读之前,最好先阅读片段文档。