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

新手问一下, 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 写一个bat,根据文件前缀创建文件夹,并将文件移动到对应前缀名称的文件夹内
  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内