麻烦将Arraylist插入片段

时间:2014-05-28 13:44:18

标签: android arraylist fragment

晚上好伙计们,我正试图在其中一些内容中使用ArrayList的滑动标签应用,但是当我尝试制作一个这样的简单应用时:

public class GamesFragment extends Fragment {
    public static ArrayList<String> messages = new ArrayList<String>();
    public static Handler mHandler = new Handler();
    private EditText recipient;
    private EditText textMessage;
    private static ListView listview;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        BusProvider.getInstance().register(this);
        View rootView = inflater.inflate(R.layout.main, container, false);

        // BusProvider.getInstance().register(this);

    // @Subscribe
    // public void anotherButtonPress(ButtonEvent event) {  
        Log.i("XMPPChatDemoActivity ", "Fragment reads me !");
        // String newtime=event.itime;
        // String newname=event.name;
        // messages.add("("+newtime+") "+newname+":");
        // messages.add(event.text);
        // setListAdapter();
        messages.add("hellooo");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),R.id.list);
        listview.setAdapter(adapter);
        return rootView;
    }
}

我收到此错误:

 FATAL EXCEPTION: main
-28 09:37:43.530: E/AndroidRuntime(5215): Process:    
com.astuetz.viewpager.extensions.sample, PID: 5215
05-28 09:37:43.530: E/AndroidRuntime(5215): java.lang.NullPointerException
05-28 09:37:43.530: E/AndroidRuntime(5215):     at   
com.astuetz.viewpager.extensions.sample.GamesFragment.on
CreateView(GamesFragment.java:61)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.FragmentManagerImpl.execPendingActions
(FragmentManager.java:1478)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at    
android.support.v4.app.FragmentManagerImpl.executePendingTransactions
(FragmentManager.java:     478)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.view.ViewPager.populate(ViewPager.java:914)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.view.View.measure(View.java:16497)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
android.view.View.measure(View.java:16497)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at 
 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
05-28 09:37:43.530: E/AndroidRuntime(5215):     at   
android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

我不明白我做错了什么,你能帮助我吗?感谢。

2 个答案:

答案 0 :(得分:0)

你还没有对listView对象进行入侵,但同样,你正在访问它。你需要

listView = (ListView) rootView.findViewById(R.id.list_view_id);

答案 1 :(得分:0)

您还没有处理 ListView

在调用Adapter方法

之前编写thid行
listview = (ListView) rootView.findViewById(R.id.????)