包含onStart方法会导致空指针异常

时间:2014-08-13 18:24:31

标签: android android-activity nullpointerexception android-service onstart

我正在尝试在我的主活动中绑定一个服务,并且正在获取空指针异常。为了绑定服务,我将onStart()和onStop()方法添加到活动中,这本身似乎导致空指针异常。这是我通过仔细评论代码行来得出的结论。

为了清楚起见,我想提供更多细节:

不会导致空指针异常:

   public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    public VivaClientService VivaClient;    
    private static final String TAG = "Viva_MainActivity";
    private Intent ClientIntent;
    private ServiceConnection ClientConnect = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                LocalBinder binder = (LocalBinder)service;
                VivaClient = binder.getService();
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
     };

    //Fragment managing the behaviors, interactions and presentation of the navigation drawer.
    private NavigationDrawerFragment mNavigationDrawerFragment;

    //Used to store the last screen title. For use in {@link #restoreActionBar()}.
    private CharSequence mTitle;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        ClientIntent = new Intent(this, VivaClientService.class);
    }

    /*
    @Override
    protected void onStart(){
        //this.bindService(ClientIntent, ClientConnect, Context.BIND_AUTO_CREATE);
        //this.startService(ClientIntent);

        //JSONObject about = VivaClient.getAbout();
        //System.out.println(about.toString());
    }


    @Override
    protected void onStop(){
    //  this.stopService(ClientIntent);
    //  this.unbindService(ClientConnect);
    }
    */

导致空指针异常:

public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    public VivaClientService VivaClient;    
    private static final String TAG = "Viva_MainActivity";
    private Intent ClientIntent;
    private ServiceConnection ClientConnect = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                LocalBinder binder = (LocalBinder)service;
                VivaClient = binder.getService();
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
     };

    //Fragment managing the behaviors, interactions and presentation of the navigation drawer.
    private NavigationDrawerFragment mNavigationDrawerFragment;

    //Used to store the last screen title. For use in {@link #restoreActionBar()}.
    private CharSequence mTitle;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        ClientIntent = new Intent(this, VivaClientService.class);
    }


    @Override
    protected void onStart(){
        //this.bindService(ClientIntent, ClientConnect, Context.BIND_AUTO_CREATE);
        //this.startService(ClientIntent);

        //JSONObject about = VivaClient.getAbout();
        //System.out.println(about.toString());
    }


    @Override
    protected void onStop(){
    //  this.stopService(ClientIntent);
    //  this.unbindService(ClientConnect);
    }

从字面上看,两者之间的唯一区别是包含了onStart和onStop方法,这些方法在包含时是空白的。我不明白为什么会导致错误。 Plz帮助; _;谢谢!

1 个答案:

答案 0 :(得分:1)

您没有从覆盖中调用super.onStart()super.onStop(),这会导致Activity抛出异常。