0

我已经制作了这个示例流程应用程序但无法运行它我不断收到这个空指针异常请告诉我如何解决它以及为什么我有它

我的主要活动课:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SimpleServiceController extends Activity {

Button start1 = (Button)findViewById(R.id.start);
Button stop1 = (Button)findViewById(R.id.stop);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startService(new     Intent(SimpleServiceController.this,Service1.class));
        }
    });




    stop1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            stopService(new Intent(SimpleServiceController.this,Service1.class));


        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

我的服务等级

 import android.app.Service;
 import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;


public class Service1 extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}


}

我的 maifest.xml

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dom.example.serviceapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SimpleServiceController"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <service android:name=".Service1" ></service>

</application>

请告诉我我哪里错了。

4

2 回答 2

6

按钮的初始化应该在onCreate()方法内部,就在之后setContentView()

start1 = (Button)findViewById(R.id.start);
stop1 = (Button)findViewById(R.id.stop);

findViewById除非您设置内容视图,否则将不起作用。

于 2012-11-13T08:26:59.130 回答
0

尝试返回一个活页夹bind(),而不是null. 例如:

// onbind example:
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
于 2012-11-13T08:45:56.337 回答