0

I am stuck of this problem. Actually, what i am doing is, inserting the records in SQLitedatabase and fetching those records to display in list view.

Here is the code :

package com.activities.sqlitedbtest;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.db.recordStore.Contact;
import com.db.recordStore.DBHelper;

public class ListManagerActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_manager);  **//edit and Uncommented this line**

        ListView listView = (ListView) findViewById(R.id.listView1); **//this line return null value..**

        Log.d("ListView :: ", ""+listView); **// printing null value**

        DBHelper helper = new DBHelper(this);
        List<Contact> list =  helper.getAllRecords();

        String[] records = new String[list.size()];

        Log.d("Records count ", ""+records.length);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_manager, records);

        listView.setAdapter(adapter);

    }
}

Also, activity_list_manager.xml is :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.activities.sqlitedbtest.ListManagerActivity"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

I am trying to display the list in a listView. On Click of Button this Activity (ListManagerActivity) is called.

This is what i did to call the another activity :

Intent i = new Intent(this,ListManagerActivity.class);
startActivity(i);

Not able to understand what i am doing wrong. In logCat shows the following error :

EDIT

08-14 20:02:20.985: D/OpenGLRenderer(2446): TextureCache::get: create texture(0xb89b1b38): name, size, mSize = 29, 7488, 1116068
08-14 20:02:21.205: D/dalvikvm(2446): GC_CONCURRENT freed 151K, 3% free 8205K/8391K, paused 12ms+1ms, total 18ms
08-14 20:02:21.209: D/ListView ::(2446): android.widget.ListView@53517528
08-14 20:02:21.225: D/Records count(2446): 2
08-14 20:02:21.245: E/ArrayAdapter(2446): You must supply a resource ID for a TextView
08-14 20:02:21.245: D/AndroidRuntime(2446): Shutting down VM
08-14 20:02:21.249: W/dalvikvm(2446): threadid=1: thread exiting with uncaught exception (group=0xa6260288)
08-14 20:02:21.253: E/AndroidRuntime(2446): FATAL EXCEPTION: main
08-14 20:02:21.253: E/AndroidRuntime(2446): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ListView.onMeasure(ListView.java:1156)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer.doFrame(Choreographer.java:525)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Handler.handleCallback(Handler.java:615)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Looper.loop(Looper.java:137)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at dalvik.system.NativeStart.main(Native Method)
08-14 20:02:21.253: E/AndroidRuntime(2446): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
08-14 20:02:21.253: E/AndroidRuntime(2446):     ... 36 more
08-14 20:02:22.717: I/Process(2446): Sending signal. PID: 2446 SIG: 9

I Googled it, but still the problem is not resolved.

I found this.

Community
  • 1
  • 1
  • setContentView is commented, so it can't load the views inside layout. – Héctor Aug 14 '14 at 19:56
  • uncomment this line **setContentView(R.layout.activity_list_manager);** and try – SaravanaRaja Aug 14 '14 at 19:57
  • Uncoment the line of set content view – geekCode Aug 14 '14 at 19:58
  • no it didn't help the problem is still there. Even i have uncomment this before dropping the question here. – user3934560 Aug 14 '14 at 19:59
  • But can any body tell me what should i do for the below two statements : setContentView(R.layout.activity_list_manager); and ArrayAdapter adapter = new ArrayAdapter(this, R.layout.activity_list_manager, records); The resources layout used is same. is that ok ???? – user3934560 Aug 14 '14 at 20:07

4 Answers4

0

You have commented the call to setContentView. Android hasn't inflated any layouts for that particular activity yet, so when you try to find the view, it returns null, because it can't find anything.

Uncomment the line below and it should be solved.

setContentView(R.layout.activity_list_manager);
Miro Markaravanes
  • 3,167
  • 21
  • 32
0

Use Following line when you Assign Arrays a value

This line is a android's inbuilt ListRow template android.R.layout.simple_list_item_1

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records);
Kirk
  • 4,604
  • 2
  • 27
  • 54
0

This line ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_manager, records); is the problem. you should not pass the layout resource id as a second argument. you mus pass a layout which represents each row or simple use this android.R.layout.simple_list_item_1.

try this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, records)
Sadegh
  • 2,469
  • 1
  • 20
  • 26
  • Thanks for the reply. But now it is showing the error related to java.lang.NullPointerException at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) – user3934560 Aug 14 '14 at 20:45
  • So it seems you are passing a null list of records! please check the record not being null before passing it into the adapter. – Sadegh Aug 14 '14 at 20:52
  • the length returned by records.length is 1. That means it has records but when used for loop to return all the values it shows error. – user3934560 Aug 14 '14 at 21:06
  • `ArrayAdapter` could not be used for a list of 'Contact` objects. You should write you own ArrayAdapter` class. ([Spend some quality time to read this](http://www.vogella.com/tutorials/AndroidListView/article.html)). for now you can just create a ArrayList(String) and add each contacts name to it by looping on your ArrayList(contacts). – Sadegh Aug 14 '14 at 21:16
  • Thanks a lot you really helped me. Accepting your answer. +10 for you. And i will read the docs which you had given by vogella, which is most useful. – user3934560 Aug 14 '14 at 21:31
  • You're welcome. ListView is actually one the most complex views in android, so you have to spend more time to learn about it. – Sadegh Aug 14 '14 at 21:37
-1

If uncomment does not help I would recommend for you using ListActivity instead of Activity.

You can read more about that here: http://developer.android.com/reference/android/app/ListActivity.html

Your ListView id in XML should be

android:id="@android:id/list""

And in code you can use

ListView listView = getListView();

taskul
  • 1
  • 2
  • No difference. It is better to find a solution instead of ignoring the question. It will help him learn what the problem was and it will keep him from making the same mistake in the future again. – Miro Markaravanes Aug 14 '14 at 20:13
  • @taluksa Could u please focus on the code in which i am using the two statements setContentView(resourceId); and in ArrayAdapter construtor passing the argument as Context, the same resource id as above and array of data i.e. (Context,resourceId,st_arr). I want to know whether i am right or wrong .. More briefly see, R.layout.activity_list_manager is used two times in the code – user3934560 Aug 14 '14 at 20:14
  • Try to look if you are not using other layout with the same name. Or R.id.listView1 from other layout. I would check these at first. – taskul Aug 14 '14 at 20:23