新手问一下, 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); 网上查了一天了,还是找不到原因,求大神们指教

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

你要看logcat的日志,它里面会清楚的告诉你那句代码空指针了

adpter的实例化在onAttach()方法里面,而你绑定数据源到适配器是在onCreateView()方法中;解决方案:adapter = new NewsAdapter(activity, R.layout.news_item, newsList);放到onCreateView()里面试试。

应该是适配器adapter为空

adapter在执行在setAdapter 的时候应该还没有实例化,onAttach在这之前应该没有执行

12-24 01:13:09.452: D/AndroidRuntime(547): Shutting down VM
12-24 01:13:09.452: W/dalvikvm(547): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-24 01:13:09.612: E/AndroidRuntime(547): FATAL EXCEPTION: main
12-24 01:13:09.612: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentbestpractice/com.example.fragmentbestpractice.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:137)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-24 01:13:09.612: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 01:13:09.612: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 01:13:09.612: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 01:13:09.612: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 01:13:09.612: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
12-24 01:13:09.612: E/AndroidRuntime(547): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-24 01:13:09.612: E/AndroidRuntime(547): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.Activity.setContentView(Activity.java:1835)
12-24 01:13:09.612: E/AndroidRuntime(547): at com.example.fragmentbestpractice.MainActivity.onCreate(MainActivity.java:14)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.Activity.performCreate(Activity.java:4465)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-24 01:13:09.612: E/AndroidRuntime(547): ... 11 more
12-24 01:13:09.612: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
12-24 01:13:09.612: E/AndroidRuntime(547): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.widget.ListView.setAdapter(ListView.java:460)
12-24 01:13:09.612: E/AndroidRuntime(547): at com.example.fragmentbestpractice.NewsTitleFragment.onCreateView(NewsTitleFragment.java:39)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:773)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1056)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.app.Activity.onCreateView(Activity.java:4243)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
12-24 01:13:09.612: E/AndroidRuntime(547): ... 21 more
12-24 01:13:11.672: I/Process(547): Sending signal. PID: 547 SIG: 9
上面日志中说的 39行就是 上面代码中的 titleListView.setAdapter(adapter);这一行啊。。

建议学会看LogCat日志,看的时候你可以把类似下面这种错误忽略:
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-24 01:13:09.612: E/AndroidRuntime(547): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

而锁定类似下面的这种错误:
12-24 01:13:09.612: E/AndroidRuntime(547): at com.example.fragmentbestpractice.NewsTitleFragment.onCreateView(NewsTitleFragment.java:39)

锁定错误后,解决错误就容易多了