0

我正在尝试在我的主要活动中绑定一个服务并且得到一个空指针异常。为了绑定服务,我在活动中添加了 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 方法,当包含它们时,它们是空白的。我不明白为什么这会导致错误。请帮助;_; 谢谢!

4

1 回答 1

1

您没有调用super.onStart()super.onStop()从您的覆盖中调用,这将导致 Activity 引发异常。

于 2014-08-13T18:32:40.960 回答