子ListView不起作用

时间:2014-06-11 19:11:25

标签: java android

我正在编写一个模拟http://www.rottentomatoes.com/mobile/网站的Android应用程序 我在listView中有一个电影列表,当我点击一部电影时我想要一个评论列表(如果你点击网站就会看到它们)但是当我点击时我的应用程序停止工作

MainActivity

public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        System.out.println ("clicked");
        Movie data = (Movie) adapter.getItem(position);
        String url = data.getLinks().getReviews();
        System.out.println (url);
        Intent intent = new Intent();
        intent.setClass(getActivity(), MovieDetailsActivity.class);
        intent.setData(Uri.parse(url));
        startActivity(intent);

    }

第二项活动 - 假设开始列表

package charnetskaya.rottentomatoes;


public class MovieDetailsActivity extends Activity {

static String url;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.movie_details);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    System.out.println("CREATED");
    Intent intent = getIntent();
    Uri uri = intent.getData();
    url = uri.toString();
}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment implements
        OnItemClickListener {

    private ReviewsAdapter adapter;

    public PlaceholderFragment() {

    }

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

        View rootView = inflater.inflate(R.layout.movie_details, container,
                false);
        ListView list = (ListView) rootView.findViewById(R.id.reviewsList);
        adapter = new ReviewsAdapter(inflater);
        list.setAdapter(adapter);
        System.out.println("HERE");
        DownloadReviews task = new DownloadReviews(url);
        task.execute(new Void[0]);
        System.out.println("THERE");
        return list;

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

}

}

错误消息 enter image description here

1 个答案:

答案 0 :(得分:1)

PlaceholderFragment onCreateView尝试返回rootView而不是list

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

        View rootView = inflater.inflate(R.layout.movie_details, container,
                false);
        ListView list = (ListView) rootView.findViewById(R.id.reviewsList);
        adapter = new ReviewsAdapter(inflater);
        list.setAdapter(adapter);
        System.out.println("HERE");
        DownloadReviews task = new DownloadReviews(url);
        task.execute(new Void[0]);
        System.out.println("THERE");
        return rootView;

    }