Tag: android

android.content.res.ResourceNotFoundException:String resource ID #0x2 in onListItemClicked

final int row_index = participant.getColumnIndex(HRADbAdapter.participant_id);
final int participant_id = participant.getInt(row_index);

TextView participant_id_tv = (TextView )new_participant_view.findViewById(R.id.patient_id);
participant_id_tv.setText(participant_id);  //this is the line that caused the above exception.

At the beginning, I was thinking it’s because participant_id_tv is null. but after looking at my laoyout file, R.id.patient_id is clearly a valid resource id, with <TextView android:id=”@+id/patient_id”…./>

It turns out it’s because when I pass an integer to setText, android thinks I am passing a resource id and tries to find resource. what I need is to explicitly convert int to String as below:

participant_id_tv.setText(Integer.toString(participant_id));

Now it’s okay. simple.

learn how to detect Swipe left / right in android

First issue: horizontal swipe is not detected in ScrollView

I implemented onFling using below code:

public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
System.out.print(“onFling is called!\n”);
// return true;
try {
if (Math.abs(e1.getY() – e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() – e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//do your code
BackHandler((Button)findViewById(R.id.btn_back));
return true;
} else if (e2.getX() – e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//left to right flip
ContinueHandler((Button)findViewById(R.id.btn_continue));
return true;
}
} catch (Exception e) {
// nothing
}
return false;
}

I used the code from a post on Stackoverflow( http://stackoverflow.com/questions/937313/android-basic-gesture-detection ). It says it’ll work. but when I put it into my project, I found only left swipe will be detected while right swipe can’t invoke any gesture listener method, including onFling, onTouchEvent, onDown.

But then I found out it’s because I used Scrollview in my layout file. If I remove the scrollview, then both left and right swipe will work. however, scrollview has to be in the layout, so what should I do? I found the solution is to override dispatchTouchEvent method of gestureListener  (http://stackoverflow.com/questions/8326599/horizontal-swipe-not-detected-in-scrollviews-parent)

@Override
public boolean dispatchTouchEvent( MotionEvent ev ) {
// TouchEvent dispatcher.
if( detector != null ) {
if( detector.onTouchEvent( ev ) )
// If the gestureDetector handles the event, a swipe has been
// executed and no more needs to be done.
return true;
}
return super.dispatchTouchEvent( ev );
}

Now swipe works in the presence of scrollview. however, i found another issue.

Problem 2: button & spinner can not be selected.

This problem doesn’t show up when the dispatchTouchEvent() method is not overrided as code above. so how to solve this problem? what I did is to change the last line  “return true” to “return false” in onDown, onTouchEvent and onScroll method in which “return true” is the default last (or only) line code in the method.

when I swipe, I found onDown is first called, then onScroll, last onFling. when nothing is done in those code, we should make them return false. Returning true “means” that the event has been consumed, and is therefore not passed on.

if the event is dealt in the current method, then return true, meaning the event is consumed, so no need to pass it on to child views;
Otherwise, return false so the event can be passed on to child views.

Testing android app on real Device

Today I got the Samsung Galaxy Note 10” tablet and can’t wait to install my uncompleted app on it.

First, I need to install USB driver for the tablet. When I plugged it in, it shows fail to find the android usb driver for ADB(Android Debug Bridge). At this time, my Eclipse can’t find the device, and neither abc. when I call “abc devices” command line, it shows no attached device.  For samsung tablet, I went to samsung website to install ADB driver. it turns out I have to install Kies from Samsung and this will install ADB driver for me. I couldn’t find independent ADB driver. Now, “adb devices” on command line will show a device connected. the number shown doesn’t make sense though. Note: on command line, first need to go to the folder of android-sdk/platform-tools/, then call adb devices.

Then, I turn the debugging mode on by Developer options->USB debugging mode

Now, in Eclipse, I run my application as usual, only to set “Run configuration”->Target->Launch on all compatible devices/avd->active devices.

Now the app is installed to the tablet.  As long as the cable is connected between the tablet and the computer, I can compile and run the app on tablet which is faster than when running on emulator. awesome!

However, I had an issue in this whole afternoon and evening. I keep running into “null pointer” error when I click on some of  elements in the questionnaire, or when I try to open an activity( in my case, questionnaire activity). and I thought maybe it’s because Samsung use different library . so I changed some elements name, or changed the way of implementing the style of spinner (set android:entries=”@array/**” instead dynamically customize it in onCreate(). all the errors are still there. they are all caused by findViewById() can’t find the element. and I couldn’t find a pattern for what elements may go wrong.  At first, i thought maybe it’s because I set some element to be android:visibility=”gone” in the xml file, and findViewById() can’t find it under Samsung library or what.

Guess what, it’s all because I created layout files of QuestionnaireActivity for different densities, but I didn’t update the version for xhdpi. I only update for mdpi (layout folder without any prefix).  But this tablet is xhdpi. thus, it is using the old layout file that doesn’t have all the elements.