2014-01-19 162 views
-4

我在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; 
    } 
    } 
+0

在這裏你去:'產生的原因:java.lang.ClassCastException:com.example.tutorial2.UserCustomAdapter' – keyser

+0

即使你是新的,你至少可以給你一些研究背景。 –

+0

您的UserCustomAdapter應該由BaseAdapter擴展。只需將公共類UserCustomAdapter {...}更新爲公共類UserCustomAdapter,即可擴展BaseAdapter {...}。請參閱此鏈接以引用它應該如何:http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – mamakurka

回答

2

您UserCustomAdapter不執行ListAdapter,這就是爲什麼你不能將其轉換爲ListAdapter。

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