如何修复此运行时错误?

时间:2014-01-19 14:41:15

标签: android android-adapter

我在Java中有这个错误:

01-19 14:08:37.402: E/AndroidRuntime(375): FATAL EXCEPTION: main
01-19 14:08:37.402: E/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial2/com.example.tutorial2.MainActivity}: java.lang.ClassCastException: com.example.tutorial2.UserCustomAdapter
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 14:08:37.402: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:08:37.402: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:507)
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 14:08:37.402: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)
01-19 14:08:37.402: E/AndroidRuntime(375): Caused by: java.lang.ClassCastException: com.example.tutorial2.UserCustomAdapter
01-19 14:08:37.402: E/AndroidRuntime(375):  at com.example.tutorial2.MainActivity.onCreate(MainActivity.java:42)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 14:08:37.402: E/AndroidRuntime(375):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-19 14:08:37.402: E/AndroidRuntime(375):  ... 11 more

这是MainActivity.java我认为这个活动是错误的。

 package com.example.tutorial2;

    import java.util.ArrayList;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListAdapter;
    import android.widget.Toast;

    public class MainActivity extends Activity {ListView userList; UserCustomAdapte userAdapter;
     ArrayList<User> userArray = new ArrayList<User>();

    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main)
  /**
   * add item in arraylist
   */
  userArray.add(new User("Mumer", "Spain", "Spain"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  /**
   * set item into adapter
   */
  userAdapter = new UserCustomAdapter(MainActivity.this, R.layout.row,
    userArray);
  userList = (ListView) findViewById(R.id.listView);
  userList.setItemsCanFocus(false);
  userList.setAdapter((ListAdapter) userAdapter);
  /**
   * get on item click listener
   */
  userList.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     final int position, long id) {
    Log.i("List View Clicked", "**********");
    Toast.makeText(MainActivity.this,
      "List View Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });

 }

User.java

package com.example.tutorial2;

public class User {
         String name;
         String address;
         String location;

         public String getName() {
          return name;
         }

         public void setName(String name) {
          this.name = name;
         }

         public String getAddress() {
          return address;
         }

         public void setAddress(String address) {
          this.address = address;
         }

         public String getLocation() {
          return location;
         }

         public void setLocation(String location) {
          this.location = location;
         }

         public User(String name, String address, String location) {
          super();
          this.name = name;
          this.address = address;
          this.location = location;
         }

        }

UserCustomAdapter.java

package com.example.tutorial2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UserCustomAdapter {
     Context context;
     int layoutResourceId;
     ArrayList<User> data = new ArrayList<User>();

     public UserCustomAdapter(Context context, int layoutResourceId,
       ArrayList<User> data) {
      super();
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.data = data;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;
      UserHolder holder = null;

      if (row == null) {
       LayoutInflater inflater = ((Activity) context).getLayoutInflater();
       row = inflater.inflate(layoutResourceId, parent, false);
       holder = new UserHolder();
       holder.textName = (TextView) row.findViewById(R.id.textView1);
       holder.textAddress = (TextView) row.findViewById(R.id.textView2);
       holder.textLocation = (TextView) row.findViewById(R.id.textView3);
       holder.btnEdit = (Button) row.findViewById(R.id.button1);
       holder.btnDelete = (Button) row.findViewById(R.id.button2);
       row.setTag(holder);
      } else {
       holder = (UserHolder) row.getTag();
      }
      User user = data.get(position);
      holder.textName.setText(user.getName());
      holder.textAddress.setText(user.getAddress());
      holder.textLocation.setText(user.getLocation());
      holder.btnEdit.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Edit Button Clicked", "**********");
        Toast.makeText(context, "Edit button Clicked",
          Toast.LENGTH_LONG).show();
       }
      });
      holder.btnDelete.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Delete Button Clicked", "**********");
        Toast.makeText(context, "Delete button Clicked",
          Toast.LENGTH_LONG).show();
       }
      });
      return row;

     }

     static class UserHolder {
      TextView textName;
      TextView textAddress;
      TextView textLocation;
      Button btnEdit;
      Button btnDelete;
     }
    }

2 个答案:

答案 0 :(得分:2)

您的UserCustomAdapter未实现ListAdapter,这就是您无法将其强制转换为ListAdapter的原因。

答案 1 :(得分:0)

使用以下

 public class UserCustomAdapter extends ArrayAdapter{ // extend ArrayAdapter 

   public UserCustomAdapter(Context context, int layoutResourceId,
   ArrayList<User> data) {
   super(context,layoutResourceId,data); // change constructor

将适配器设置为

 userList.setAdapter(userAdapter);