定制阵列适配器问题

时间:2012-07-31 20:28:25

标签: android

我正在使用android中的一个小聊天室,现在当我设置自定义ListView时,我收到以下错误,有什么想法吗?

public class roomActivity extends Activity
{
    ListView mainListView;
    ProgressDialog progressDialog;
    String[] jsonUsernameArray, jsonMessageArray;
    UpdateTask mTask = null;

    @Override
    public void onCreate(Bundle activityCycle)
    {
        super.onCreate(activityCycle);
        setContentView(R.layout.room_layout);
        mainListView = (ListView)findViewById(R.id.roomListView);
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Fetech the data...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        mTask = new UpdateTask();
        mTask.execute();
    }

    public class UpdateTask extends AsyncTask<Void, Void, JSONObject> 
    {
        JSONArray _roomData = null;
        protected void onPostExecute(JSONObject jsonResult)
        {
            try 
            {
                _roomData = jsonResult.getJSONArray("roomInfo");
                for (int cnt = 0; cnt < _roomData.length(); cnt++)
                {
                    JSONObject c = _roomData.getJSONObject(cnt);
                    jsonUsernameArray[cnt] = c.getString("name");
                    jsonMessageArray[cnt] = c.getString("message");
                }
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }
            mainListView.setAdapter(new customAdapter());
            progressDialog.dismiss();
        }

        @Override
        protected JSONObject doInBackground(Void... backgroundProcess)
        {
            UserFunctions userFunction = new UserFunctions();
            return userFunction.sendMessageToROOM("50", "Reza", "Salam dadash :-)", "89988");    
        }   
    }

    public class customAdapter extends BaseAdapter
    {

        public int getCount() 
        {
            return jsonMessageArray.length;
        }

        public Object getItem(int itemData) 
        {
            return null;
        }

        public long getItemId(int itemPosition) 
        {
            return itemPosition;
        }

        public View getView(int _position, View mainView, ViewGroup mainViewGroup) 
        {
            LayoutInflater maniInFlater = getLayoutInflater();
            View _row = null;
            TextView _usernameText, _messageText;
            if (_position % 2 == 0)
            {
                _row = maniInFlater.inflate(R.layout.conversion_layout_odd, mainViewGroup, false);
                _usernameText = (TextView)_row.findViewById(R.id.txtUsername);
                _messageText = (TextView)_row.findViewById(R.id.txtMessage);
                _usernameText.setText(jsonUsernameArray[_position]);
                _messageText.setText(jsonMessageArray[_position]);  
            }
            else
            {
                //do sth...
            }
            return _row;
        }   

    }
}

Logcat错误:

08-01 00:53:04.065: E/AndroidRuntime(17114): FATAL EXCEPTION: main
08-01 00:53:04.065: E/AndroidRuntime(17114): java.lang.NullPointerException
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.merlingames.thermotalk.roomActivity$UpdateTask.onPostExecute(roomActivity.java:52)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.merlingames.thermotalk.roomActivity$UpdateTask.onPostExecute(roomActivity.java:1)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask.finish(AsyncTask.java:602)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.os.Looper.loop(Looper.java:137)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 00:53:04.065: E/AndroidRuntime(17114):    at dalvik.system.NativeStart.main(Native Method)

已编辑:

08-01 01:26:07.260: E/AndroidRuntime(18381):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 01:27:36.327: W/dalvikvm(18870): threadid=1: thread exiting with uncaught exception (group=0x40a561f8)
08-01 01:27:36.327: E/AndroidRuntime(18870): FATAL EXCEPTION: main
08-01 01:27:36.327: E/AndroidRuntime(18870): java.lang.NullPointerException
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.AbsListView.obtainView(AbsListView.java:2024)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.ListView.onMeasure(ListView.java:1155)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.View.measure(View.java:12723)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.os.Looper.loop(Looper.java:137)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 01:27:36.327: E/AndroidRuntime(18870):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您应该添加此行来初始化数组:

_roomData = jsonResult.getJSONArray("roomInfo"); 

//Add this line
jsonUsernameArray = new String[_roomData.length()];

答案 1 :(得分:1)

您的数组jsonUsernameArray永远不会被初始化。了解如何正确使用数组:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html