簡體   English   中英

在連接了Android的Google App Engine項目上創建自定義ArrayAdapter

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

我在與Android連接的Google App Engine項目上部署自定義arrayadapter時遇到嚴重問題。 即使在我調試應用程序時在鍵入代碼時沒有錯誤,因為使用模擬器的Android應用程序也會導致錯誤,並且應用程序停止。 我的自定義適配器代碼在下面,根據調試器,在第46行之后有NullPointerException 。這是什么以及如何解決它?

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;
    }

}

這是調用適配器的活動:

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 
}

這是錯誤日志...

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...

謝謝您的幫助。

您永遠不會設置標簽值,因此當您執行holder = (ListHolder)row.getTag(); 它返回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

編輯:現在除了您的日志抱怨SuperNotCalled。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM