I have spent the last two days trying to figure this out. I have a service that I am creating to practice with services and IPC
. I have checked the code against the book that I am reading it is almost exact only difference being identifiers. I get a ResourcesNotFoundException
thrown in the mainwindow.java
file. It is thrown when I try to use my IBinder
. Any tip in the right direction will be much appreciated.
mainwindow.java
package com.evilOrion;
import com.evilOrion.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class mainwindow extends Activity {
TextView tView = null;
Button tButton = null;
private adder service;
private boolean bound;
private ServiceConnection connection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder iservice){
service = adder.Stub.asInterface(iservice);
bound = true;
}
public void onServiceDisconnected(ComponentName className){
bound = false;
service = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tView = (TextView)findViewById(R.id.TextView);
tView.setText("hi");
tButton = (Button)findViewById(R.id.Button01);
tButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(v.equals(tButton)){
try{
int result = service.add(5, 7);// crashes on this line with no exception
tView.setText(result);
}catch(DeadObjectException e){
Log.i("DeadObjectException", "found the problem");
}
catch(RemoteException e){
Log.i("Exception: " , "shoot!");
}
//tView.setText("What a wonderful day it is");
}
}
});
}
public void onStart(){
super.onStart();
if(!bound){
this.bindService(new Intent(mainwindow.this, AdderService.class), connection, Context.BIND_AUTO_CREATE);
}
}
public void onPause(){
super.onPause();
if(bound){
bound = false;
this.unbindService(connection);
}
}
}
AdderService.java
package com.evilOrion;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AdderService extends Service {
private final adder.Stub binder = new adder.Stub() {
@Override
public int subtranct(int a, int B) throws RemoteException {
// TODO Auto-generated method stub
return a - b;
}
@Override
public String echo(String input) throws RemoteException {
// TODO Auto-generated method stub
return "echo" + input;
}
@Override
public int add(int a, int B) throws RemoteException {
// TODO Auto-generated method stub
return a + b;
}
};
public IBinder onBind(Intent it){
return this.binder;
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.evilOrion"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".mainwindow"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AdderService"
android:label="@string/app_name"></service>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Here"></Button>
</LinearLayout>
My Manifest
file does have the <service>
so I am sure it is not there. I have gone literally line by line between my book and the code and I see 0 differences. I just don't understand what I am doing wrong. One other odd side question. What creates the connection? I get ServiceConnection
holds the information on my connection with the two methods onServiceConnected
and onServiceDisconnected
, but the only place I use connection is in onStart()
, does onStart()
automatically get called after all other methods are complete. Essentially, I am lost I usually avoid asking questions but I haven't been able to find this answer.
int result = service.add(5, 7); in the mainwindow.java file causes:
No package identifier when getting value for resource number 0x0000000c
09-14 20:36:08.684: INFO/Exception!!!(12446): android.content.res.Resources$NotFoundException: String resource ID #0xc
is thrown in the mainwindow.java
try{
int result = service.add(5, 7);// crashes on this line with no exception
tView.setText(result);
}catch(DeadObjectException e){
Log.i("DeadObjectException", "found the problem");
}
Occam's razor in effect. Sometimes you eat the bar and sometimes the bar eats you.
tView
doesn't take int values.
Answer:
tView.setText(String.valueOf(result);
Boy do I feel sheepish a week to find that.
User contributions licensed under CC BY-SA 3.0