2016-05-12 101 views
0

我收到一個錯誤,當我嘗試調用主活動類上的服務的任何方法。android服務方法調用調用空引用錯誤

錯誤日誌

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String in.ac.iitkgp.alpha.dbService.getUser()' on a null object reference 
                    at in.ac.iitkgp.alpha.MainScreen.onCreateOptionsMenu(MainScreen.java:95) 
                    at android.app.Activity.onCreatePanelMenu(Activity.java:2846) 
                    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341) 
                    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                    at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454) 
                    at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61) 
                    at android.os.Handler.handleCallback(Handler.java:739) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我有兩個活動第一活動將輸入到服務並使用startService(意圖)啓動它;並調用下一個活動。然後,下一個活動綁定到服務並讀取輸入數據並更改UI上的TextView。 我需要通過服務傳遞它,因爲許多活動都需要輸入。

Service類

public class dbService extends Service { 

private final IBinder Mybinder = new dbBinder(); 
public String user; 
public dbService() { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return Mybinder; 
} 

public final String getUser(){ 
    return user; 
} 


public class dbBinder extends Binder{ 
    dbService getService(){ 
     return dbService.this; 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    user = intent.getStringExtra("User"); 
    return START_NOT_STICKY; 
} 
} 

從第一活動

Intent intent = new Intent(this,MainScreen.class); 
    Intent service = new Intent(this,dbService.class); 
    String usr = user.getText().toString(); 
    service.putExtra("User",usr); 
    startService(service); 
    startActivity(intent); 

片段片段從第二活動

dbService Myservice; 
boolean bound = false; 
private ServiceConnection connection = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     dbBinder binder = (dbBinder) service; 
     Myservice = binder.getService(); 
     bound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     bound = false; 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
. 
. 
. 
Intent i = new Intent(this,dbService.class); 
    bindService(i,connection, Context.BIND_AUTO_CREATE); 
    TextView user = (TextView) findViewById(R.id.user); 
    user.setText(Myservice.getUser()); 
} 

僅訪問服務的方法從第二活動崩潰的應用程序。其他一切似乎都很好。 對不起,如果這是一個愚蠢的問題。我是新來的Java和Android應用程序開發。 在此先感謝

回答

0

把下面的代碼在onServiceConnected

TextView user = (TextView) findViewById(R.id.user); user.setText(Myservice.getUser());` 

除非服務獲取連接,則無法訪問服務對象的方法。

+0

不應該bindService已經連接活動到服務和它下面的代碼應該在服務連接後執行? – Ash

+0

不,您應該先連接,然後才能使用它。在連接引用我的服務對象之前,我可以看到它。顯然它會拋出一個NPE – Sush

相關問題