2012-11-13 51 views
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> 

請告訴我我在哪裏錯了。

+3

請發表您的logcat錯誤 – omi0301

回答

6

按鈕的初始化應該是裏面onCreate()方法,剛過setContentView()

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

findViewById不會工作,除非你設置的內容視圖。

+0

非常感謝上帝保佑你。你讓我的程序工作 –

+0

歡迎你:) –

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(); 
+0

我的問題是由第一個答案解決,但仍然謝謝你 –