0

メイン アクティビティでサービスをバインドしようとしていますが、ヌル ポインター例外が発生します。サービスをバインドするために、onStart() および onStop() メソッドをアクティビティに追加しましたが、それ自体が null ポインター例外を引き起こしているようです。これは、コード行を慎重にコメントアウトすることで得た結論です。

明確にするために、さらに詳細を提供すると思います。

Null ポインタ例外は発生しません:

   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);
    }
    */

Null ポインター例外が発生します。

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);
    }

文字通り、2 つの唯一の違いは、含まれている場合は空である onStart メソッドと onStop メソッドが含まれていることです。なぜエラーが発生するのかわかりません。助けてください;_; ありがとう!

4

1 に答える 1

1

アクティビティが例外をスローする原因となるオーバーライドから、super.onStart()または呼び出していません。super.onStop()

于 2014-08-13T18:32:40.960 に答える