0

我正在使用基于此链接上的@fedor 帖子的列表视图和惰性适配器构建应用程序

这是我的第一个活动:

public class MenuViewAll extends ListActivity {

// url to make request
private static String url = "http://10.0.2.2/culigui/getdataresto.php";
//public static String TAG_NAME,TAG_ADDRESS;
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_ADDRESS = "address";
static final String KEY_THUMB = "linkthumb";

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_view_all);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting JSONArray of listresto
        JSONArray  listresto = json.getJSONArray("listresto");

        // looping through All listresto
        for(int i = 0; i < listresto.length(); i++){
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject list = listresto.getJSONObject(i);


            // insert String to Local Variable
            map.put(KEY_ID, list.getString("id_resto"));
            map.put(KEY_NAME, list.getString("nama_resto"));
            map.put(KEY_ADDRESS, list.getString("alamat_resto"));
            map.put(KEY_THUMB, list.getString("thumb_img"));
            userList.add(map);


        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */



    adapter = new LazyAdapter(this, userList);
    list.setAdapter(adapter);

   // ListView yourListView = getListView() 

}

这是我的懒惰适配器类:

public class LazyAdapter extends BaseAdapter  {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listitemviewall, null);

    TextView namaresto = (TextView)vi.findViewById(R.id.name); // resto name
    TextView alamatresto = (TextView)vi.findViewById(R.id.address); // resto address
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.defaultthumb); // thumb image

    HashMap<String, String> resto = new HashMap<String, String>();
    resto = data.get(position);

    // Setting all values in listview
    namaresto.setText(resto.get(MenuViewAll.KEY_NAME));
    alamatresto.setText(resto.get(MenuViewAll.KEY_ADDRESS));
    imageLoader.DisplayImage(resto.get(MenuViewAll.KEY_THUMB), thumb_image);
    return vi;
}}

在我运行我的代码之后,这里是 logcat:

04-05 14:51:32.714: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 869 objects / 60304 bytes in 76ms
04-05 14:51:46.235: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 757 objects / 43592 bytes in 362ms
04-05 14:51:46.945: D/AndroidRuntime(311): Shutting down VM
04-05 14:51:46.945: W/dalvikvm(311): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-05 14:51:46.975: E/AndroidRuntime(311): FATAL EXCEPTION: main
04-05 14:51:46.975: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{last.project.CuliGUI/last.project.CuliGUI.MenuViewAll}: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
04-05 14:51:46.975: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 14:51:46.975: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
04-05 14:51:46.975: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 14:51:46.975: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 14:51:46.975: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at     last.project.CuliGUI.MenuViewAll.onCreate(MenuViewAll.java:102)

我的程序总是强制关闭...也许有人可以帮助我...

4

1 回答 1

1

您应该注意这部分错误:

 04-05 14:51:46.975: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311):  at     last.project.CuliGUI.MenuViewAll.onCreate(MenuViewAll.java:102)

特别是:

MenuViewAll.java:102

这意味着该行中的某些内容(我不会数行:))是NULL,但是您尝试使用它。寻找可能的对象的使用NULL

示例(只是一个示例,因为我不知道它是哪一行)。让我们说这一行:

JSONObject list = listresto.getJSONObject(i);

由于某种原因出错。(没有JSONobject i,listresto 是假的,我不知道)。然后在这个之后:

        map.put(KEY_ID, list.getString("id_resto"));

将给出一个空指针,因为您不能getStringNULL对象执行此操作。

所以:

  • 找到线
  • 找到对象
  • 修复它为 NULL 的原因,或者确保你事先处理它
  • ...
  • 利润
于 2012-04-05T10:03:11.040 回答