使用CustomListAdapter时出现NullPointerException

时间:2014-09-17 18:09:43

标签: java android nullpointerexception listactivity

我在Android Appiclation中创建CustomListAdapter。但是在实现它时我得到了这个错误NullPointerException

这是我的CustomListAdapter.java代码:

/**
 * 
 */
package com.fanjavaid.searchhttprequest.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanjavaid.searchhttprequest.R;
import com.fanjavaid.searchhttprequest.app.AppController;
import com.fanjavaid.searchhttprequest.model.Menu;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author fanjavaid
 *
 */
public class CustomListAdapter extends BaseAdapter {

    private Activity activity;

    private LayoutInflater inflater;

    private List<Menu> menuItems;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Menu> menuItems) {
        this.activity = activity;
        this.menuItems = menuItems;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return menuItems.size();
    }

    @Override
    public Object getItem(int location) {
        // TODO Auto-generated method stub
        return menuItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) 
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
            NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
            TextView menuTitle = (TextView) convertView.findViewById(R.id.menuTitle);
            TextView inputDate = (TextView) convertView.findViewById(R.id.postDate);

        // Getting movie data for the row
        Menu m = menuItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

        // title
        menuTitle.setText(m.getMenuTitle());

        // input date
        inputDate.setText("Post Date : " + String.valueOf(m.getInputDate().toString()));

        return convertView;
    }

}

这是我的活动代码:

package com.fanjavaid.searchhttprequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fanjavaid.searchhttprequest.adapter.CustomListAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class ListMenuActivity extends Activity {

    // Log tag
    private static final String TAG = ListMenuActivity.class.getSimpleName();

    // Movie JSON Url
    private List<com.fanjavaid.searchhttprequest.model.Menu> menuList = new ArrayList<com.fanjavaid.searchhttprequest.model.Menu>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_menu);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, menuList);
        listView.setAdapter(adapter);

        Intent intent = getIntent();

        //String jsonData = intent.getStringExtra("json_menu");

        Bundle bundle = intent.getExtras();
        String jsonString = bundle.getString("json_menu");

        try {
            JSONObject objectJson = new JSONObject(jsonString);
            JSONArray arrayJson = objectJson.getJSONArray("results");

            for (int i = 0; i < arrayJson.length(); i++) {

                JSONObject obj = arrayJson.getJSONObject(i);

                com.fanjavaid.searchhttprequest.model.Menu menu = new com.fanjavaid.searchhttprequest.model.Menu();
                menu.setMenuTitle(obj.getString("nama_menu"));
                menu.setThumbnailUrl("http://tempercube/receip_finder/" + obj.getString("gambar"));

                String dateInString = obj.getString("tgl_input");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date parsedDate = formatter.parse(dateInString);
                menu.setInputDate(parsedDate);

                // add to list
                menuList.add(menu);
            }

            // notifying list adapter about data changed
            // so that it renders the list view with data changed
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的活动中的setAdapter()方法之前已经添加了CustomListAdapter实例化。 但我仍然得到NullPointerException错误。

这是我的错误logcat

09-18 09:11:34.661: E/AndroidRuntime(24097): FATAL EXCEPTION: main
09-18 09:11:34.661: E/AndroidRuntime(24097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fanjavaid.searchhttprequest/com.fanjavaid.searchhttprequest.ListMenuActivity}: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.os.Looper.loop(Looper.java:137)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.main(ActivityThread.java:5214)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invoke(Method.java:525)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at dalvik.system.NativeStart.main(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097): Caused by: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.fanjavaid.searchhttprequest.adapter.CustomListAdapter.<init>(CustomListAdapter.java:34)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at com.fanjavaid.searchhttprequest.ListMenuActivity.onCreate(ListMenuActivity.java:38)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.Activity.performCreate(Activity.java:5133)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-18 09:11:34.661: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
09-18 09:11:34.661: E/AndroidRuntime(24097):    ... 11 more

这是我的AppController.java

/**
 * 
 */
package com.fanjavaid.searchhttprequest.app;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.fanjavaid.searchhttprequest.util.LruBitmapCache;

import android.app.Application;
import android.text.TextUtils;

/**
 * @author fanjavaid
 *
 */
public class AppController extends Application {
    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

这一个,我的MainActivity.java

package com.fanjavaid.searchhttprequest;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    private TextView responseText;
    private EditText searchText;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        responseText = (TextView) findViewById(R.id.textView1);
        searchText = (EditText) findViewById(R.id.searchBox);

        url = "http://tempercube.com/receip_finder/process_cari.php";
    }

    private String addLocationToUrl(String url, String parameter) {
        if (!url.endsWith("?"))
            url += "?";

        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("bahan", parameter));

        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += paramString;

        return url;
    }

    public void doRequest(String passUrl) {
        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Method.GET, passUrl, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        // TODO Auto-generated method stub
                        Log.d("TAG", response.toString());
                        responseText.setText(response.toString());

                        Intent intent = new Intent("com.fanjavaid.searchhttprequest.ListMenuActivity");

                        Bundle extras = new Bundle();
                        extras.putString("json_menu", response.toString());

                        intent.putExtras(extras);

                        startActivity(intent);

                        pDialog.hide();
                    }

                },

                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("ERROR", "Error : " + error.getMessage());
                        pDialog.hide();
                    }

                });

        // Make request queue
        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);

    }

    public void searchMenu(View view) {
        if(view.getId() == R.id.button1){
            String paramSearch = searchText.getText().toString();
            url = addLocationToUrl(url, paramSearch);

            Log.d("URL_INFO", url);

            doRequest(url);

            // reset URL
            url = "http://tempercube.com/receip_finder/process_cari.php";
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

最后我的list_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView 
        android:id="@+id/list" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector">
    </ListView>

</RelativeLayout>

请帮忙。非常感谢你。

3 个答案:

答案 0 :(得分:21)

我认为您没有将AppController添加到您的清单中:

 <application
        android:name=".AppController" //this one
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

这就是为什么onCreate从未调用过,mInstance为空。

答案 1 :(得分:0)

我注意到您使用private List<com.fanjavaid.searchhttprequest.model.Menu> menuList = new ArrayList<com.fanjavaid.searchhttprequest.model.Menu>();,但在致电之前,您实际上从未向该列表添加任何内容:

listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, menuList);
listView.setAdapter(adapter);

你可能想在menuList

中加入一些东西

答案 2 :(得分:0)

将AppController(活动请求)添加到您的清单:

 <application
    android:name=".AppController" //this one
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">