tags:

views:

187

answers:

1

Been trying to find out what is the bookmark id the user clicked on... Tried everything, many force closes... and now an empty toast (no error marks in eclipse):

public class Dmarks extends ListActivity {

    protected Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView selection;
        String[] projection = new String[] {
                                    Browser.BookmarkColumns._ID,
                                    Browser.BookmarkColumns.FAVICON, 
                                    Browser.BookmarkColumns.TITLE, 
                                    Browser.BookmarkColumns.URL
                                    };
        String[] displayFields = new String[] {
                                    Browser.BookmarkColumns.TITLE, 
                                    Browser.BookmarkColumns.FAVICON,
                                    Browser.BookmarkColumns.URL
                                    };
        final int[] displayViews = new int[] {
                                    android.R.id.text1, 
                                    android.R.id.text2
                                    };

        Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI, projection, android.provider.Browser.BookmarkColumns.BOOKMARK, null, null);

        setListAdapter(new ImageCursorAdapter(this, android.R.layout.simple_list_item_2, cur, displayFields, displayViews));
        selection = (TextView)findViewById(R.id.btitle);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Context context = getApplicationContext();
                TextView text = selection;
                int duration = Toast.LENGTH_SHORT;

                Toast.makeText(context, (CharSequence) text, duration).show();
          }
        });

   }

ImageCursorAdapter is another class showing bookmarks favicon and title (it works).

Really would appreciate help with what am I doing wrong here.

Thanks!

A: 

You are casting a TextView into a CharSequence? That doesn't make any sense. How about text.getText().

You should also learn to use logcat or eclipse ddms to get stack traces or additional information when your app force closes. From eclipse, open the logcat window. Or, run adb logcat on the command line.

Mayra
I'm guessing you mean text.getText() instead of (CharSequence)text - tried that and got force close :\Opened logcat and it says I have an error in line 60 which is the text.getText() line...
liorry
Can you print the exact error message? It is definitly an error to cast a TextView to a CharSequence. My first guess is that text is null.
Mayra
D/AndroidRuntime( 3568): Shutting down VMW/dalvikvm( 3568): threadid=1: thread exiting with uncaught exception (group=0x4001d7e8)E/AndroidRuntime( 3568): FATAL EXCEPTION: mainE/AndroidRuntime( 3568): java.lang.NullPointerExceptionE/AndroidRuntime( 3568): at com.droidil.droidmarks.Dmarks$1.onItemClick(Dmarks.java:60)E/AndroidRuntime( 3568): at android.widget.AdapterView.performItemClick(AdapterView.java:284)E/AndroidRuntime( 3568): at android.widget.ListView.performItemClick(ListView.java:3382)It's too long :\
liorry
E/AndroidRuntime( 3568): at java.lang.reflect.Method.invokeNative(NativeMethod)E/AndroidRuntime( 3568): at java.lang.reflect.Method.invoke(Method.java:521)E/AndroidRuntime( 3568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)E/AndroidRuntime( 3568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)E/AndroidRuntime( 3568): at dalvik.system.NativeStart.main(Native Method)W/ActivityManager( 131): Force finishing activity com.droidil.droidmarks/.Dmarks
liorry
java.lang.NullPointerException: This means that you are attempting to access a null object, in this case "text" is null. I think what you actually want to be doing is locating the TexView you care about from the "view" param in onItemClicked.
Mayra
Isn't "selection = (TextView)findViewById(R.id.btitle);" doing that?
liorry
No, that is finding a view with id btitle in your general Activity layout, not the instance of the view within a list item.
Mayra
Ok Thanks, will look into it again... maybe i need to change my list structure to simple textview with image.
liorry
Can't seems to find a solution for this... getting null from every combination I've tried... Any change you point me for the right syntax? or a similar example?
liorry
I assume R.id.btitle is a TextView within R.layout.simple_list_item_2? Try, within onItemClick view.findViewById(R.id.btitle). This looks for a view with the id that is a child view of "view", and thus within that list item.
Mayra
OH! finally! :) Thank you very much. managed to get ID and using the .getText() I also got each clicked item text :)THANKS!
liorry