自定义适配器getView中的Java.lang.NullPointException

时间:2013-12-12 06:06:36

标签: listview android-listview adapter baseadapter

我在运行应用程序时遇到问题,但我收到通知,但我的应用程序已被停止

当我检查Log cat时遇到问题java.lang nullpoiint异常

这是我的历史记录适配器

package com.example.globalmedia.adapter;

import com.example.globalmedia.History;
import com.example.globalmedia.R;
import com.example.globalmedia.image.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;

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

public class HistoryAdapter extends BaseAdapter{

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public HistoryAdapter(Context context, 
            ArrayList<HashMap<String, String>> arraylist){
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {

        return data.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TextView id_movies, title, year, tgl_expired, type, link;
        ImageView thumb;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View history = inflater.inflate(R.layout.history_item, parent,false);
        resultp = data.get(position);

        id_movies  = (TextView) history.findViewById(R.id.id_movies);
        title = (TextView) history.findViewById(R.id.history);
        year = (TextView) history.findViewById(R.id.year);
        tgl_expired = (TextView) history.findViewById(R.id.tgl_expired);
        type = (TextView) history.findViewById(R.id.type);
        link = (TextView) history.findViewById(R.id.link);
        thumb = (ImageView) history.findViewById(R.id.thumb_new);

        title.setText(resultp.get(History.TITLE));
        id_movies.setText(resultp.get(History.ID_MOVIES));
        year.setText(resultp.get(History.YEAR));
        tgl_expired.setText(resultp.get(History.TGLEXPIRED));
        type.setText(resultp.get(History.TYPE));
        link.setText(resultp.get(History.LINK));    

        imageLoader.DisplayImage(resultp.get(History.THUMBNEW), thumb);

        return history;
    }
}

这里是logcat

12-12 01:01:20.660: E/AndroidRuntime(2438): FATAL EXCEPTION: main
12-12 01:01:20.660: E/AndroidRuntime(2438): java.lang.NullPointerException
12-12 01:01:20.660: E/AndroidRuntime(2438):     at com.example.globalmedia.adapter.HistoryAdapter.getView(HistoryAdapter.java:70)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.ListView.onMeasure(ListView.java:1159)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.TableLayout.measureVertical(TableLayout.java:476)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.TableLayout.onMeasure(TableLayout.java:439)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.View.measure(View.java:15848)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.os.Handler.handleCallback(Handler.java:730)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.os.Looper.loop(Looper.java:137)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at java.lang.reflect.Method.invoke(Method.java:525)
12-12 01:01:20.660: E/AndroidRuntime(2438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Z

历史课

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.globalmedia.adapter.HistoryAdapter;
import com.example.globalmedia.server.JSONfunctions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.view.Menu;

public class History extends Activity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    HistoryAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;

    public static String ID_MOVIES = "id_movies";
    public static String TITLE = "title";
    public static String THUMBNEW = "thumb_new";
    public static String YEAR = "year";
    public static String TGLEXPIRED = "tgl_expired";
    public static String TYPE = "type";
    public static String LINK = "link";

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

        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(History.this);
            // Set progressdialog message
            mProgressDialog.setMessage("Loading Movie...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            arraylist = new ArrayList<HashMap<String, String>>();

            jsonobject = JSONfunctions.getJSONfromURL("http://192.168.182.10/android/history.php?id_member=6");

            try {
                jsonarray = jsonobject.getJSONArray("history");

                for (int i = 0; i<jsonarray.length(); i++){
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    map.put("id_movies", jsonobject.getString("id_movies"));
                    map.put("title", jsonobject.getString("title"));
                    map.put("thumb_new", jsonobject.getString("thumb_new"));
                    map.put("year", jsonobject.getString("year"));
                    map.put("tgl_expired", jsonobject.getString("tgl_expired"));
                    map.put("type", jsonobject.getString("type"));
                    map.put("link", jsonobject.getString("link"));

                    arraylist.add(map);
                }
            } catch (JSONException e){
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args){

            listview = (ListView) findViewById(R.id.history);
            adapter = new HistoryAdapter(History.this, arraylist);
            listview.setAdapter(adapter);
            mProgressDialog.dismiss();
        }

    }

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

3 个答案:

答案 0 :(得分:1)

尝试如下,它可能适合你。

    title.setText(data.get(position).get(History.TITLE));
    id_movies.setText(data.get(position).get(History.ID_MOVIES));
    year.setText(data.get(position).get(History.YEAR));
    tgl_expired.setText(data.get(position).get(History.TGLEXPIRED));
    type.setText(data.get(position).get(History.TYPE));
    link.setText(data.get(position).get(History.LINK));  
   imageLoader.DisplayImage(data.get(position).get(History.THUMBNEW), thumb);  

<强>编辑:

更改您的getView()方法,如下所示:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View history = convertView;
        ViewHolder m_holder;
        if (history == null) {
             inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             history = inflater.inflate(R.layout.history_item, parent,false);
             m_holder = new ViewHolder();

             m_holder.id_movies  = (TextView) history.findViewById(R.id.id_movies);
             m_holder. title = (TextView) history.findViewById(R.id.history);
             m_holder.year = (TextView) history.findViewById(R.id.year);
             m_holder.tgl_expired = (TextView) history.findViewById(R.id.tgl_expired);
             m_holder.type = (TextView) history.findViewById(R.id.type);
             m_holder.link = (TextView) history.findViewById(R.id.link);
             m_holder.thumb = (ImageView) history.findViewById(R.id.thumb_new);


        } else {
            m_holder = (ViewHolder) history.getTag();
        }


        m_holder.title.setText(data.get(position).get(History.TITLE));
        m_holder.id_movies.setText(data.get(position).get(History.ID_MOVIES));
        m_holder.year.setText(data.get(position).get(History.YEAR));
        m_holder. tgl_expired.setText(data.get(position).get(History.TGLEXPIRED));
        m_holder.type.setText(data.get(position).get(History.TYPE));
        m_holder.link.setText(data.get(position).get(History.LINK));    

        imageLoader.DisplayImage(data.get(position).get(History.THUMBNEW), m_holder.thumb);

        return history;
    }

    private class ViewHolder {
        private TextView id_movies, title, year, tgl_expired, type, link;
         private ImageView thumb;
    }

答案 1 :(得分:1)

<强>建议:

根据你所做的代码,我建议你在getView()方法中实现ViewHolder模式。

详细了解Making ListView scrolling smooth

答案 2 :(得分:0)

使用ViewHolder进行平滑滚动。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

您需要了解listview如何回收视图。

How ListView's recycling mechanism works

static class ViewHolder
{
    TextView id_movies,title,year,tgk_expired,type,link;
    ImageView thumb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if(convertView==null)
    {
        holder = new ViewHolder();
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.convertView_item, parent,false);
        holder.id_movies  = (TextView) convertView.findViewById(R.id.id_movies);
        holder.title = (TextView) convertView.findViewById(R.id.convertView);
        holder.year = (TextView) convertView.findViewById(R.id.year);
        holder.tgl_expired = (TextView) convertView.findViewById(R.id.tgl_expired);
        holder.type = (TextView) convertView.findViewById(R.id.type);
        holder.link = (TextView) convertView.findViewById(R.id.link);
        holder.thumb = (ImageView) convertView.findViewById(R.id.thumb_new);
        convertView.setTag(holder); 
    }
    else
    {
        holder = (ViewHolder)convertView.getTag();
    }
    resultp = data.get(position);

    holder.title.setText(resultp.get(History.TITLE));
    holder.id_movies.setText(resultp.get(History.ID_MOVIES));
    holder.year.setText(resultp.get(History.YEAR));
    holder.tgl_expired.setText(resultp.get(History.TGLEXPIRED));
    holder.type.setText(resultp.get(History.TYPE));
    holder.link.setText(resultp.get(History.LINK));    

    imageLoader.DisplayImage(resultp.get(History.THUMBNEW), holder.thumb);

    return convertView;
}