j250912392h
2015-12-23 02:47
采纳率: 85.7%
浏览 2.6k

新手问一下, android中setAdapter的空指针问题

代码如下:
package com.example.fragmentbestpractice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
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.ListView;

public class NewsTitleFragment extends Fragment implements OnItemClickListener {
private ListView titleListView;
private List newsList;
private NewsAdapter adapter;
private boolean isTwoPane;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    newsList = getNews();
    adapter = new NewsAdapter(activity, R.layout.news_item, newsList);
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater
            .inflate(R.layout.news_title_frag, container, false);
    titleListView = (ListView) view
            .findViewById(R.id.title_list);
    titleListView.setAdapter(adapter);
    titleListView.setOnItemClickListener(this);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    if (getActivity().findViewById(R.id.news_content_layout) != null) {
        isTwoPane = true;
    } else {
        isTwoPane = false;
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
        long id) {
    News news = newsList.get(position);
    if (isTwoPane) {
        NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager()
                .findFragmentById(R.id.news_content_fragment);
        newsContentFragment.refresh(news.getTitle(), news.getContent());
    } else {
        NewsContentActivity.actionStart(getActivity(), news.getTitle(),
                news.getContent());
    }
}

public List<News> getNews() {
    List<News> newslist = new ArrayList<News>();
    News news1 = new News();
    news1.setTitle("hahahaahhaahahahhahahaha");
    news1.setContent("hohohohohohhohohohohohohohohohohoho" +
            "hohohohohohohohohohohohohohohohohohohohohohohoh");
    newslist.add(news1);
    News news2 = new News();
    news2.setTitle("qoqoqoqoqqoqoqoqoqoqoqoqoqoqo");
    news2.setContent("bobobobobobobbobobobbo" +
            "bobobobobobobobobobobbobobobobbobobobobob" +
            "bobobooboobobobbobhahahahdhdhabobobobobobobobob");
    newslist.add(news2);
    return newsList;

}

}
日志中错误提示在这一句中: titleListView.setAdapter(adapter); 网上查了一天了,还是找不到原因,求大神们指教

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

7条回答 默认 最新

  • cindyCode 2015-12-23 07:30
    最佳回答

    小兄弟,建议你初始化的初始化数据的操作写在onCreate()方法里面。理解你为什么写在onAttach()中但容易发生错误,你最好吧LogCat打印出来的信息发出来看看,报的错我认为应该不是adapter空指针错误,你看看打印出来含有Cause by的那一行是什么错误,下面应该就是错误代码所在的地方,个人认为是你的adapter里面有问题,OnAttach()是Fragment生命周期的开始,在那里初始化不存在adapter为Null的情况。fragment生命周期图

    评论
    解决 无用
    打赏 举报
查看更多回答(6条)

相关推荐 更多相似问题