为了账号安全,请及时绑定邮箱和手机立即绑定

空指针异常

空指针的问题咋解决,我换了action方式也不行.startService取不到服务器里面的Service类 startService(new Intent(this,Class.forName("aidl.msd.com.aidldemo.AidlService")));像这样也不行

正在回答

2 回答

另外服务端要注册服务,并且允许远程,不然会发生安全性异常

<service android:name=".MService"
        android:process=":remote"
        android:exported="true"
   />

2 回复 有任何疑惑可以回复我~

package com.jin.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jin.appspare.AidlObj;
import com.jin.appspare.IMyAidlInterface;
import com.jin.appspare.Person;

import java.util.List;

public class MainActivity extends AppCompatActivity {

   private EditText mEt1;
   private EditText mEt2;
   private TextView mTv;
   private Button mBt;
   private IMyAidlInterface iMyAidlInterface;
   private AidlObj aidlObj;
   private ServiceConnection conn = new ServiceConnection() {
       @Override
       public void onServiceConnected(ComponentName name, IBinder service) {
           Log.e("client", "服务已经连接" + name);
           //拿到远程的服务-----------------------------------------------
//            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
           aidlObj = AidlObj.Stub.asInterface(service);
       }

       @Override
       public void onServiceDisconnected(ComponentName name) {
           Log.e("client", "服务已经断开" + name);
           aidlObj = null;
       }
   };

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       initView();
       bindService();
       initEvent();

   }

   private void initView() {
       mEt1 = (EditText) findViewById(R.id.editText);
       mEt2 = (EditText) findViewById(R.id.editText2);
       mTv = (TextView) findViewById(R.id.textView);
       mBt = (Button) findViewById(R.id.button);


   }

   private void initEvent() {
       mBt.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
//                try {
//                    int number1 = Integer.parseInt(mEt1.getText().toString());
//                    int number2 = Integer.parseInt(mEt2.getText().toString());
//                    int add = iMyAidlInterface.add(number1, number2);
//                    mTv.setText(add + "");
//                }catch (Exception e) {
//                    e.printStackTrace();
//                }


               try {

                //---------------------------------------------------------------------

                   List<Person> abc = aidlObj.add(new Person("ABC", 20));
                   for (Person person : abc) {
                       Log.e("person-name", person.getName());
                       Log.e("person-age", person.getAge() + "");
                   }

            //---------------------------------------------------------------------

               } catch (RemoteException e) {
                   e.printStackTrace();
               }

           }
       });
   }


   private void bindService() {
       //获取服务端
       Intent intent = new Intent();
       //显示Intent启动绑定服务
       intent.setComponent(new ComponentName("com.jin.appspare","com.jin.appspare.MService"));
       //一绑定,就会启动
       Log.e("client","开始绑定服务端");
       bindService(intent, conn, Context.BIND_AUTO_CREATE);
   }

   @Override
   protected void onDestroy() {
       super.onDestroy();
       unbindService(conn);
   }
}

0 回复 有任何疑惑可以回复我~
#1

慕姐1217228 提问者

我试下看,你的可以么
2018-03-07 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
AIDL-小白成长记
  • 参与学习       42558    人
  • 解答问题       57    个

AIDL-小白成长记,想要快速入门,看本次的教程就对了

进入课程

空指针异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信