2016-05-12 99 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

相关问题