使用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个回答

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

wuzg1020
wuzg1020 现在再看
接近 4 年之前 回复

客户端和服务端的包名不一致,肯定不行

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问