pager.setAdapter()处的空指针异常

时间:2015-07-05 19:49:54

标签: android tabs viewer

我在获取空指针异常 pager.setAdapter(tabpager);

这是代码,请检查一下,让我知道错误

主要活动的Java代码

    package com.example.prototype;


import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener  {
    TabsPagerAdapter tabpager;
    ViewPager pager;
    DrawerLayout drawerLayout;      
    ActionBar action_bar;
    ListView navigation_list;
    @SuppressWarnings("deprecation")
    ActionBarDrawerToggle drawerToggle;
     String items[];
     String tabs_list[]={"Best Offers","Categories","Near By Places"};

       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);
        initComponents();
        action_bar=getActionBar();
        setNavigationList();
        setTabs();
    }

    @SuppressWarnings("deprecation")
    private void setTabs() {

        action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (String tab_name : tabs_list) {
            action_bar.addTab(action_bar.newTab().setText(tab_name)
                    .setTabListener(this));
        }
        try{pager.setAdapter(tabpager);}
        catch(Exception e){
            e.printStackTrace();


        }

    }

    @SuppressWarnings("deprecation")
    private void setNavigationList() {

        int open=R.string.navigation_open;
        int close=R.string.navigation_close;
         navigation_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

         drawerToggle=new ActionBarDrawerToggle(this,drawerLayout, R.drawable.ic_menu_white_18dp,open,close){


             public void onDrawerClosed(View view) {
                 super.onDrawerClosed(view);
                 action_bar.show();
             }

             public void onDrawerOpened(View drawerView) {
                 super.onDrawerOpened(drawerView);
                 action_bar.hide();

             }
             };




            drawerLayout.setDrawerListener(drawerToggle);

            action_bar.setDisplayHomeAsUpEnabled(true);
            action_bar.setHomeButtonEnabled(true);


        }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }


    private void initComponents() {
        // TODO Auto-generated method stub

         drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
         action_bar=getActionBar();
         navigation_list=(ListView) findViewById(R.id.left_drawer);
         items=getResources().getStringArray(R.array.navigation_drawer);
         pager=(ViewPager) findViewById(R.id.pager);    
         android.support.v4.app.FragmentManager m=getSupportFragmentManager();
         tabpager=new TabsPagerAdapter(m);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.search_menu) {
            Toast.makeText(getApplicationContext(), "Searched Clicked ", Toast.LENGTH_LONG).show();
            return true;
        }

        if (id == R.id.share_menu) {
            Toast.makeText(getApplicationContext(), "Share Clicked ", Toast.LENGTH_LONG).show();
            return true;
        }

        if (id == R.id.navigation_menu) {
            Toast.makeText(getApplicationContext(), "Navigation Clicked ", Toast.LENGTH_LONG).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }



}

适用于AdapterClass

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {

        switch(arg0){
        case 0:   new BestOfferFragment();
                    break;
        case 1: new CategoriesrFragment();
        break;

        case 2: new NearByPlacesFragment();
        break;
        }

        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">





<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true" />





     <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="5dp"
        android:background="#E1E1E1"/>
</android.support.v4.widget.DrawerLayout>    

请帮帮我

我检查了Viewer和Tabadapter的对象,但它们不是null 我不明白为什么它在设置适配器时出错..

这是Log

07-06 01:24:16.471: D/dalvikvm(20183): threadid=2: interp stack at 0x5a6b7000
07-06 01:24:16.476: D/dalvikvm(20183): threadid=3: interp stack at 0x5a7bf000
07-06 01:24:16.477: D/jdwp(20183): prepping for JDWP over ADB
07-06 01:24:16.477: D/jdwp(20183): ADB transport startup
07-06 01:24:16.479: D/dalvikvm(20183): Elevating priority from 0 to -8
07-06 01:24:16.479: D/dalvikvm(20183): threadid=4: interp stack at 0x5a8c7000
07-06 01:24:16.480: D/jdwp(20183): JDWP: thread running
07-06 01:24:16.482: D/jdwp(20183): acceptConnection
07-06 01:24:16.483: D/jdwp(20183): trying to receive file descriptor from ADB
07-06 01:24:16.484: D/dalvikvm(20183): threadid=5: interp stack at 0x5cd1d000
07-06 01:24:16.484: D/dalvikvm(20183): zygote get thread init done
07-06 01:24:16.484: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.484: D/dalvikvm(20183): create new thread
07-06 01:24:16.484: D/dalvikvm(20183): new thread created
07-06 01:24:16.485: D/dalvikvm(20183): update thread list
07-06 01:24:16.485: D/dalvikvm(20183): threadid=6: interp stack at 0x5cd25000
07-06 01:24:16.485: D/dalvikvm(20183): threadid=6: created from interp
07-06 01:24:16.485: D/dalvikvm(20183): start new thread
07-06 01:24:16.486: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.486: D/dalvikvm(20183): create new thread
07-06 01:24:16.487: D/dalvikvm(20183): new thread created
07-06 01:24:16.487: D/dalvikvm(20183): update thread list
07-06 01:24:16.487: D/dalvikvm(20183): threadid=6: notify debugger
07-06 01:24:16.487: D/dalvikvm(20183): threadid=6 (ReferenceQueueDaemon): calling run()
07-06 01:24:16.487: D/dalvikvm(20183): threadid=7: interp stack at 0x5ce2d000
07-06 01:24:16.488: D/dalvikvm(20183): threadid=7: created from interp
07-06 01:24:16.488: D/dalvikvm(20183): start new thread
07-06 01:24:16.490: D/dalvikvm(20183): create interp thread : stack size=32KB
07-06 01:24:16.490: D/dalvikvm(20183): create new thread
07-06 01:24:16.490: D/jdwp(20183): received file descriptor 39 from ADB
07-06 01:24:16.490: D/dalvikvm(20183): new thread created
07-06 01:24:16.490: D/dalvikvm(20183): update thread list
07-06 01:24:16.490: D/dalvikvm(20183): threadid=7: notify debugger
07-06 01:24:16.490: D/dalvikvm(20183): threadid=7 (FinalizerDaemon): calling run()
07-06 01:24:16.491: D/dalvikvm(20183): threadid=8: interp stack at 0x5cf35000
07-06 01:24:16.491: D/dalvikvm(20183): threadid=8: created from interp
07-06 01:24:16.491: D/dalvikvm(20183): start new thread
07-06 01:24:16.495: D/jdwp(20183): processIncoming
07-06 01:24:16.495: D/jdwp(20183): processIncoming
07-06 01:24:16.495: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000019F, flags=0x0, dataLen=0x8
07-06 01:24:16.504: D/jdwp(20183): processIncoming
07-06 01:24:16.504: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x400001A0, flags=0x0, dataLen=0xC
07-06 01:24:16.516: D/jdwp(20183): processIncoming
07-06 01:24:16.517: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001A1, flags=0x0, dataLen=0x8
07-06 01:24:16.525: D/dalvikvm(20183): threadid=8: notify debugger
07-06 01:24:16.525: D/dalvikvm(20183): threadid=8 (FinalizerWatchdogDaemon): calling run()
07-06 01:24:16.537: D/jdwp(20183): processIncoming
07-06 01:24:16.537: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400001A2, flags=0x0, dataLen=0x8
07-06 01:24:16.538: D/jdwp(20183): processIncoming
07-06 01:24:16.538: D/jdwp(20183): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x400001A3, flags=0x0, dataLen=0x9
07-06 01:24:16.576: D/dalvikvm(20183): threadid=9: interp stack at 0x5d33b000
07-06 01:24:16.591: D/dalvikvm(20183): threadid=10: interp stack at 0x5d443000
07-06 01:24:16.614: D/jdwp(20183): sendBufferedRequest : len=0x3D
07-06 01:24:16.720: D/jdwp(20183): sendBufferedRequest : len=0x45
07-06 01:24:16.770: D/dalvikvm(20183): open_cached_dex_file : /data/app/com.example.prototype-2.apk /data/dalvik-cache/data@app@com.example.prototype-2.apk@classes.dex
07-06 01:24:16.807: D/skia(20183): Flag is not 10
07-06 01:24:16.813: D/skia(20183): Flag is not 10
07-06 01:24:16.817: D/skia(20183): Flag is not 10
07-06 01:24:16.824: D/skia(20183): Flag is not 10
07-06 01:24:16.825: D/skia(20183): Flag is not 10
07-06 01:24:16.827: D/skia(20183): Flag is not 10
07-06 01:24:16.828: D/skia(20183): Flag is not 10
07-06 01:24:16.845: D/skia(20183): Flag is not 10
07-06 01:24:16.857: D/dalvikvm(20183): GC_FOR_ALLOC freed 109K, 8% free 2861K/3092K, paused 11ms, total 11ms
07-06 01:24:16.859: I/dalvikvm-heap(20183): Grow heap (frag case) to 3.982MB for 1127536-byte allocation
07-06 01:24:16.876: D/dalvikvm(20183): GC_FOR_ALLOC freed 1K, 6% free 3961K/4196K, paused 17ms, total 17ms
07-06 01:24:16.884: D/skia(20183): Flag is not 10
07-06 01:24:16.893: D/skia(20183): Flag is not 10
07-06 01:24:16.896: D/dalvikvm(20183): GC_CONCURRENT freed 3K, 6% free 3969K/4196K, paused 2ms+1ms, total 20ms
07-06 01:24:16.899: D/skia(20183): Flag is not 10
07-06 01:24:16.900: D/skia(20183): Flag is not 10
07-06 01:24:16.901: D/skia(20183): Flag is not 10
07-06 01:24:16.902: D/skia(20183): Flag is not 10
07-06 01:24:16.904: D/AbsListView(20183): checkAbsListViewlLogProperty get invalid command
07-06 01:24:16.907: D/skia(20183): Flag is not 10
07-06 01:24:16.910: D/skia(20183): Flag is not 10
07-06 01:24:16.914: D/skia(20183): Flag is not 10
07-06 01:24:16.916: D/skia(20183): Flag is not 10
07-06 01:24:16.917: D/skia(20183): Flag is not 10
07-06 01:24:16.918: D/skia(20183): Flag is not 10
07-06 01:24:16.919: D/skia(20183): Flag is not 10
07-06 01:24:16.976: D/AndroidRuntime(20183): Shutting down VM
07-06 01:24:16.976: W/dalvikvm(20183): threadid=1: thread exiting with uncaught exception (group=0x4126e9a8)
07-06 01:24:16.980: E/AndroidRuntime(20183): FATAL EXCEPTION: main
07-06 01:24:16.980: E/AndroidRuntime(20183): java.lang.NullPointerException
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:417)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:412)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1019)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.View.measure(View.java:15609)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.Choreographer.doFrame(Choreographer.java:548)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.os.Handler.handleCallback(Handler.java:725)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.os.Looper.loop(Looper.java:153)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at android.app.ActivityThread.main(ActivityThread.java:5297)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 01:24:16.980: E/AndroidRuntime(20183):    at dalvik.system.NativeStart.main(Native Method)
07-06 01:24:17.008: I/Process(20183): Sending signal. PID: 20183 SIG: 9

3 个答案:

答案 0 :(得分:2)

您的适配器getItem()未返回任何非空值。返回空值会导致问题。

请注意,您使用new实例化新片段,但您没有对刚刚实例化的片段进行任何操作。

要解决此问题,请更改

new WhateverFragment();

return new WhateverFragment();

适用于适配器中的所有三个片段。

答案 1 :(得分:1)

getItem返回片段:

@Override
public Fragment getItem(int arg0) {
    Fragment fragment = null;
    switch(arg0){
    case 0:   fragment = new BestOfferFragment();
                break;
    case 1: fragment = new CategoriesrFragment();
    break;

    case 2: fragment = new NearByPlacesFragment();
    break;
    }

    return fragment;
}

答案 2 :(得分:0)

您正在创建新项目,但不会将其返回:

@Override
public Fragment getItem(int arg0) {
    Fragment resultFragment = null;
    switch(arg0){
    case 0:  
      resultFragment = new BestOfferFragment();
    break;
    case 1: 
      resultFragment = new CategoriesrFragment();
    break;
    case 2: 
      resultFragment = new NearByPlacesFragment();
    break;
    }

    return resultFragment;
}