0

I am attempting to create a custom messaging app. Just something basic but I am getting some error that I have never heard of. Tried for 2 days to fix it so I thought I would bother you guys with it. This is the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView messages = (ListView) findViewById(R.id.messages);
    msgList = getSMS(); 
    for(int i = 0; i<msgList.size(); i++) {
        System.out.println(msgList.get(i));
    }
    adapter = new ArrayAdapter<String>(MainActivity.this, 0, R.layout.list_item, msgList);
    **messages.setAdapter(adapter);**
    messages.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int pos,long id) 
        {
               SmsManager m = SmsManager.getDefault();
               String destinationNumber=(String) ((TextView) view).getText();              
               m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);
        }           
   });
}

public List<String> getSMS() {      
    List<String> messages = new ArrayList<String>();
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor c = null;
    try{
        c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null); 
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
            final String address = c.getString(c.getColumnIndex("address"));
            final String body = c.getString(c.getColumnIndexOrThrow("body"));
            messages.add("Number: " + address + ". Message: " + body);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
 c.close(); 
 return messages;
}

And here is the error:

12-22 18:49:24.752: ERROR/AndroidRuntime(2054): FATAL EXCEPTION: main
12-22 18:49:24.752: ERROR/AndroidRuntime(2054): android.content.res.Resources$NotFoundException: Resource ID #0x0
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.content.res.Resources.getValue(Resources.java:892)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.content.res.Resources.getLayout(Resources.java:731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.ListView.onMeasure(ListView.java:1109)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.View.measure(View.java:8171)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1731)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.os.Looper.loop(Looper.java:123)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at java.lang.reflect.Method.invoke(Method.java:521)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-22 18:49:24.752: ERROR/AndroidRuntime(2054):     at dalvik.system.NativeStart.main(Native Method)

I know the error is occurring when I set the adapter, but I have checked the messages are being retrieved fine and I cannot seem to find where the error is occurring. Any ideas on the error(s) are greatly appreciated!

EDIT: Yep, Sorry here is the list_item.xml. If it helps, I have had a similar program working on a 2.1 phone, but throws this error on 2.2. Haven't tried this app on 2.1, however.

   <?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="15dp"
    android:textSize="22sp" >
</TextView>
1
  • Can you post your list_item.xml? Commented Dec 22, 2010 at 19:06

1 Answer 1

3

Try changing your ArrayAdapter constructor line to:

adapter = new ArrayAdapter<String>(this, R.layout.list_item, msgList);  

In your original line, you're telling the ArrayAdapter to inflate a layout with an ID of 0 (which doesn't exist), and then find a TextView within with an ID of R.id.list_item. Your TextView doesn't have an ID set, so you need to use the constructor that takes in only the TextView resource ID. The other constructor you would use for more complex ListView items where you may have multiple views within.

Alternately, you could give your TextView an ID, such as "@+id/text", then use:

adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, msgList);
Sign up to request clarification or add additional context in comments.

2 Comments

YES! Thank you, that solved it! Weird that it worked on an older version of Android but that was only in the emulator. Thanks, I will think twice now, when I use ArrayAdapters.
Cool! :) Yeah, they're tricky to get the hang of when you're just learning to use them. Glad it helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.