1

Здравствуйте. Помогите разобраться, в чём ошибка, почему NullPointer. Задача: отправляю запрос POST, приходит ответ в виде массива. Нужно его занести в ListView (кастомный). XML: listmsg.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:paddingLeft="8dip"
    android:paddingRight="8dip">

    <ListView
        android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stackFromBottom="true"
        android:transcriptMode="normal"
        android:id="@+id/list"/>

  <RelativeLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:id="@+id/ed_but">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="button_click"
        android:text="send" />

    <EditText
        android:id="@+id/usertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button" >

        <requestFocus />
    </EditText>

    </RelativeLayout>
</LinearLayout>

Элементы Listview: list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
  android:orientation="horizontal"
  android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is text1"
            android:textSize="16dip" />

        <TextView
            android:id="@+id/dddd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/author"
            android:layout_toRightOf="@+id/author"
            android:text="--> "
            android:textSize="12dip" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/dddd"
            android:text="This is "
            android:textSize="16dip" />

        <TextView
            android:id="@+id/time"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/text"
            android:gravity="right"
            android:text="10.12.2011,16:40"
            android:textSize="8dip" />

    </RelativeLayout>

</LinearLayout>

Главное активити:

public class ListviewActivity extends Activity 
{

    protected static final LayoutInflater mInflater = null;
       private String AUTHORKEY;
       private String TEXTKEY;
    private String TIMEKEY;
       private String[] names = {AUTHORKEY, TEXTKEY, TIMEKEY};
       int[] to_id = {R.id.author, R.id.text, R.id.time};

    @Override
    public void onCreate(Bundle savedInstanceState)        
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listmsg);        
        ListView list = (ListView)findViewById(R.id.list);

        MySimpleAdapter adapter = new MySimpleAdapter(this, R.layout.list, names, to_id);
        list.setAdapter(adapter);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    }
 }

Адаптер:

 public class MySimpleAdapter extends SimpleAdapter {
        private final Activity context;
        private String[] name;
        private int[] tolayout;
         static ArrayList<Map<String, Object>> data;

        public MySimpleAdapter(Activity context, int resource, String[] n, int[] to) {
            super(context, data, resource, n, to);
            // TODO Auto-generated constructor stub
            this.context = context;
            this.name = n;

            data = new ArrayList<Map<String, Object>>();
            HashMap<String, Object> hm;
            String s = (String) POST.rebutmsg();
            String [] t = s.split("%");
             int i;

              for(i=0; i<=59;i++){

                  hm = new HashMap<String, Object>();

                                  hm.put(name[0], t[i]);
                                  i++;
                                  hm.put(name[1], t[i]);
                                  i++;
                                  hm.put(name[2], t[i]);
                                  data.add(hm);
                                 }

        }
      }

Вот ошибки:

02-14 19:27:04.353: W/dalvikvm(10707): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-14 19:27:04.363: E/AndroidRuntime(10707): FATAL EXCEPTION: main
02-14 19:27:04.363: E/AndroidRuntime(10707): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fkn.chat/com.fkn.chat.ListviewActivity}: java.lang.NullPointerException
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.os.Looper.loop(Looper.java:123)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at java.lang.reflect.Method.invokeNative(Native Method)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at java.lang.reflect.Method.invoke(Method.java:507)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at dalvik.system.NativeStart.main(Native Method)
02-14 19:27:04.363: E/AndroidRuntime(10707): Caused by: java.lang.NullPointerException
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.widget.ListView.setAdapter(ListView.java:454)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at com.fkn.chat.ListviewActivity.onCreate(ListviewActivity.java:55)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 19:27:04.363: E/AndroidRuntime(10707):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 19:27:04.363: E/AndroidRuntime(10707):    ... 11 more
1
  • 1
    У вас в массиве names значения не равны null? 15 фев 2012 в 19:45

1 ответ 1

0

Да, я с помощью debug увидел свою ошибку. Я ничего не заношу в AUTHORKEY, TEXTKEY, TIMEKEY. Ошибка по невнимательности. Спасибо за помощь.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.