简体   繁体   中英

Create a custom ArrayAdapter on an android connected Google App Engine project

I having a crucial problem on deploying a custom arrayadapter on android connected google app engine project. Even though there are no errors while typing the code when I debug the application as an Android app with the emulator causes an error and the application stops. My custom adapter code is below and according the debugger there is NullPointerException after line 46. What is this and how to fix it?

package com.cheapchase;

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

public class MyListAdapter extends ArrayAdapter<ListItem>{

    Context context;
    int layoutResourceId;
    ListItem data[] = null;

    public MyListAdapter(Context context, int layoutResourceId, ListItem[] data){

        super(context,layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

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

        View row = convertView;
        ListHolder holder = null;

        if(row == null){

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ListHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        }
        else
        {
            holder = (ListHolder)row.getTag();
        }

        ListItem listrow = data[position];
        holder.txtTitle.setText(listrow.title);
        holder.imgIcon.setImageResource(listrow.icon);

        return row;
    }

    static class ListHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }

}

This is the activity calling the adapter:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class StoresList extends Activity{

    /**
     * The current context.
     */

    private ListView listView1;

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_list);
    }

    public void onResume(){

        super.onResume();

        ListItem storelist[] = new ListItem[]{
                new ListItem(R.drawable.androidmsg, "test1"),
                new ListItem(R.drawable.androidmsg, "test2")
        };


        MyListAdapter adapter = new MyListAdapter(this, R.layout.listview_item_row, storelist);

        listView1 = (ListView)findViewById(R.id.listView1);

        /*View header = (View)getLayoutInflater().inflate(R.layout.listview_item_header, null);
        listView1.addHeaderView(header);*/

        listView1.setAdapter(adapter);
    }



// **** end of file 
}

This is the error log...

06-28 15:16:44.687: W/dalvikvm(1584): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-28 15:16:44.706: E/AndroidRuntime(1584): FATAL EXCEPTION: main
06-28 15:16:44.706: E/AndroidRuntime(1584): java.lang.RuntimeException: Unable to resume activity {com.cheapchase/com.cheapchase.StoresList}: android.app.SuperNotCalledException: Activity {com.cheapchase/com.cheapchase.StoresList} did not call through to super.onResume()
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.os.Looper.loop(Looper.java:130)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at java.lang.reflect.Method.invoke(Method.java:507)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at dalvik.system.NativeStart.main(Native Method)
06-28 15:16:44.706: E/AndroidRuntime(1584): Caused by: android.app.SuperNotCalledException: Activity {com.cheapchase/com.cheapchase.StoresList} did not call through to super.onResume()
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.Activity.performResume(Activity.java:3834)
06-28 15:16:44.706: E/AndroidRuntime(1584):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
06-28 15:16:44.706: E/AndroidRuntime(1584):     ... 12 more
06-28 15:17:19.967: W/ActivityThread(1617): Application com.cheapchase is waiting for the debugger on port 8100...

Thank you for your help.

You never set the tag value, so when you do holder = (ListHolder)row.getTag(); it returns null .

        holder = new ListHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        row.setTag(holder);  //<--- missing this

Edit: Except now your log complains about SuperNotCalled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM