android AIDL 调用空指针,求高手解答
今天写了一个AIDL的需求,但是调用AIDL文件方法异常,请哪位高手,帮忙看一下,谢谢!!!
①taobaoLoginClientA
com.taobao.platformservice
---ITBPlatformService.aidl
com.taobao.taobaologinclient
---LoginContext.java
---MainActivity.java
interface ITBPlatformService { boolean isLogin(); boolean executeLogin(); void Logout(); }
public class LoginContext { public static String packageName ="com.taobao.taobaologinclient";}
public class MainActivity extends Activity { private EditText usernameET, passwordET; private TextView textView; private Button loginB, logoutB; private ITBPlatformService itbPlatformService; private ServiceConnection connection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub itbPlatformService = null; } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub itbPlatformService = ITBPlatformService.Stub.asInterface(service); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); usernameET = (EditText) this.findViewById(R.id.editText1); passwordET = (EditText) this.findViewById(R.id.editText2); textView = (TextView) this.findViewById(R.id.textView1); loginB = (Button) this.findViewById(R.id.button1); logoutB = (Button) this.findViewById(R.id.button2); Intent intent = new Intent(); intent.setAction("com.taobao.platformservice.TBPlatformService"); bindService(intent, connection, Service.BIND_AUTO_CREATE); try { //空指针地方 if(itbPlatformService.isLogin()){ textView.setText("已登陆"); }else{ textView.setText("未登陆"); } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } loginB.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub String username = usernameET.getText().toString().trim(); String password = passwordET.getText().toString().trim(); if (username == null || password == null) { Toast.makeText(getApplicationContext(), "请重新输入账号与密码", Toast.LENGTH_SHORT).show(); usernameET.setText(""); passwordET.setText(""); } try { itbPlatformService.executeLogin(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); logoutB.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub try { itbPlatformService.Logout(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); this.unbindService(connection); try { itbPlatformService.Logout(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
interface ITBPlatformService { boolean isLogin(); boolean executeLogin(); void Logout(); }
public class AppLoginInfo { private String username; private String password; private String packageName; private String appKey; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } public String getAppKey() { return appKey; } public void setAppKey(String appKey) { this.appKey = appKey; }}
public class LoginHttp { private AppLoginInfo appLoginInfo; private String AppKey = "1234"; public LoginHttp(AppLoginInfo _appLoginInfo) { this.appLoginInfo = _appLoginInfo; } public String Login() { if (appLoginInfo.getUsername() == "hhy" && appLoginInfo.getPassword() == "hhy") { return AppKey; } else { return null; } }}
public class TBPlatformService extends Service { private boolean LoginFlag = false; private AppLoginInfo appLoginInfo; private LoginBinder loginBinder; private String username; private String password; private String packageName; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i("hhy", "TBPlatformService.onBind()"); this.username = intent.getStringExtra("username"); this.password = intent.getStringExtra("password"); this.packageName = intent.getStringExtra("packageName"); appLoginInfo = new AppLoginInfo(); appLoginInfo.setUsername(username); appLoginInfo.setPassword(password); appLoginInfo.setPackageName(packageName); Log.i("hhy", username + "---" + password + "---" + packageName); return loginBinder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.i("hhy", "TBPlatformService.onCreate()"); loginBinder = new LoginBinder(); } public class LoginBinder extends Stub { public boolean isLogin() throws RemoteException { Log.i("hhy", "TBPlatformService.isLogin()"); return LoginFlag; } public boolean executeLogin() throws RemoteException { Log.i("hhy", "TBPlatformService.executeLogin()"); LoginHttp loginHttp = new LoginHttp(appLoginInfo); appLoginInfo.setAppKey(loginHttp.Login()); if (appLoginInfo.getAppKey() != null) LoginFlag = true; return LoginFlag; } public void Logout() throws RemoteException { // TODO Auto-generated method stub Log.i("hhy", "TBPlatformService.Logout()"); LoginFlag = false; } }}