j250912392h 2015-12-23 02:47 采纳率: 85.7%
浏览 2593
已采纳

新手问一下, 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条)

报告相同问题?

悬赏问题

  • ¥15 锂电池供电怎么设计?
  • ¥15 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序