wuzg1020 2016-08-30 10:30 采纳率: 5.6%
浏览 1932

使用aidl,服务端和客户端不能正常通信?

服务端目录结构:
图片说明
服务端Service代码:
package com.example.remoteservice;

import java.util.LinkedList;
import java.util.List;

import com.example.remoteservice.aidl.IMyService;
import com.example.remoteservice.aidl.Person;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class RemoteService extends Service {

private LinkedList<Person> personList = new LinkedList<Person>();  

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

private final IMyService.Stub mBinder = new IMyService.Stub(){  

        @Override  
        public void savePersonInfo(Person person) throws RemoteException {  
                if (person != null){  
                        personList.add(person);  
                }  
        }  

        @Override  
        public List<Person> getAllPerson() throws RemoteException {  
                return personList;  
        }  

        @Override  
        public String sayHello() throws RemoteException {  
            // TODO Auto-generated method stub  
            return "欢迎你通过AIDL访问服务器端";  
        }  
};  

}
服务端配置文件:
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.remoteservice.MainActivity"
android:label="@string/app_name" >

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>



    <service 
        android:name="com.example.remoteservice.RemoteService"
        >
        <intent-filter >
            <action android:name="com.example.remoteservice.aidl.IMyService"/>
        </intent-filter>
    </service>  
</application>

客户端主活动:
package com.example.remoteclient;

import java.util.List;

import com.example.remoteservice.aidl.IMyService;
import com.example.remoteservice.aidl.Person;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private TextView textHello,textPerson;  
    private IMyService myService;  
    private Button btnSave;  
    private Button btnGet;  
    private static Boolean mIsRemoteBound=false;  
    private ServiceConnection conn=new ServiceConnection() {  

        @Override  
        public void onServiceDisconnected(ComponentName name) {  
            // TODO Auto-generated method stub  
            myService=null;  
        }  

        @Override  
        public void onServiceConnected(ComponentName name, IBinder service) {  
            // TODO Auto-generated method stub  
                myService=IMyService.Stub.asInterface(service);       
            try {  
                textHello.setText(myService.sayHello());  
            } catch (RemoteException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    };  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        textHello=(TextView) this.findViewById(R.id.textHello);  
        btnSave=(Button) this.findViewById(R.id.btnSave);  
        btnGet=(Button) this.findViewById(R.id.btnGet);  
        textPerson=(TextView) this.findViewById(R.id.textPerson);  
        if(mIsRemoteBound){  
            unbindService(conn);  
        }else{  
        Intent intent=new Intent("com.example.remoteservice.aidl.IMyService");  
        bindService(intent, conn, BIND_AUTO_CREATE);  
        }  
        mIsRemoteBound = !mIsRemoteBound;  
        btnSave.setOnClickListener(new OnClickListener() {  
              private int index = 0;  

            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                   Person person = new Person();  
                   index = index + 1;  
                   person.setName("Person" + index);  
                   person.setAge(20);  
                   person.setTelNumber("123456");   
                   try {  
                       myService.savePersonInfo(person);  //
                   } catch (RemoteException e) {  
                           e.printStackTrace();  
                   }   
            }  
        });  
        btnGet.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                  List<Person> list = null;   

                  try {  
                          list = myService.getAllPerson();  
                  } catch (RemoteException e) {  
                          e.printStackTrace();  
                  }   

                  if (list != null){  
                          StringBuilder text = new StringBuilder();  

                          for(Person person : list){  
                                  text.append("\n联系人:");  
                                  text.append(person.getName());  
                                  text.append("\n             年龄:");  
                                  text.append(person.getAge());  
                                  text.append("\n 电话:");  
                                  text.append(person.getTelNumber());  
                          }  

                          textPerson.setText(text);  
                  }else {  
                          Toast.makeText(MainActivity.this, "得到数据出错",  
                                          Toast.LENGTH_SHORT).show();  
                  }   
            }  
        });  
    }  

}
客户端目录结构:
图片说明

客户端配置文件里我没有注册活动,可以吗?
运行时需要先运行服务端程序,然后Home键再运行客户端程序,结果客户端调用不了接口中方法,怎么办?
logcat:08-30 06:28:38.300: E/AndroidRuntime(781): at com.example.remoteclient.MainActivity$2.onClick(MainActivity.java:77)
图片说明
点击添加报错崩溃
错误日志:
08-30 10:35:09.760: E/AndroidRuntime(791): FATAL EXCEPTION: main
08-30 10:35:09.760: E/AndroidRuntime(791): Process: com.example.remoteclient, PID: 791
08-30 10:35:09.760: E/AndroidRuntime(791): java.lang.NullPointerException
08-30 10:35:09.760: E/AndroidRuntime(791): at com.example.remoteclient.MainActivity$2.onClick(MainActivity.java:77)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.view.View.performClick(View.java:4424)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.view.View$PerformClick.run(View.java:18383)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.os.Handler.handleCallback(Handler.java:733)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:95)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:137)
08-30 10:35:09.760: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:4998)
08-30 10:35:09.760: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 10:35:09.760: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:515)
08-30 10:35:09.760: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
08-30 10:35:09.760: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
08-30 10:35:09.760: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)

  • 写回答

2条回答 默认 最新

  • Exploring1024 2016-08-30 14:12
    关注

    你的错误日志全部贴出来,现在看不出来

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题