java.lang.classcastexception:android.widget.Linearlayout $ params

时间:2013-06-27 05:43:57

标签: android listview exception android-linearlayout android-framelayout

我正在尝试将数据库中的数据加载到List视图中。而ListView在Framelayout中。

这是我的布局文件,

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="60dp"
    android:gravity="bottom" 
    android:orientation="horizontal">

    <ListView
        android:id="@+id/list_customers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal" >

    </ListView>

    <TextView
        android:id="@+id/txtsimple_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>
</LinearLayout>

</FrameLayout>

这是我的适配器类,

 public class ListCustomersAdapter extends BaseAdapter {

public Context ctx;
public String[] customers;
LayoutInflater mInflater;

public ListCustomersAdapter(Context c,
        String[] Customers ){
    super();
    mInflater = (LayoutInflater) c
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.ctx=c;
    this.customers=Customers;
}

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

@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) {

    View localView;
    if (convertView == null) {
        localView = mInflater
                .inflate(R.layout.activity_main, null);
    } else {
        localView = convertView;
    }

    TextView txtCustomername=(TextView)localView.findViewById(R.id.txtsimple_list);
    txtCustomername.setText(customers[position]);
    txtCustomername.setLayoutParams(new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.WRAP_CONTENT,
               LinearLayout.LayoutParams.WRAP_CONTENT));
    return txtCustomername;
}
}

这是我的活动类,我将适配器设置为listView,

 public class MainActivity extends Activity {

ListView lsCustomers;
ListCustomersAdapter adapter;
public Context context = MainActivity.this;
ArrayList<String> results;
String[] Customers;
Button btnaddcustomers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    results = new ArrayList<String>();
    btnaddcustomers=(Button)findViewById(R.id.btnadd);

    Customers = new String[results.size()];
    for (int i = 0; i < results.size(); i++) {
        Customers[i] = results.get(i);
    }
    adapter = new ListCustomersAdapter(context, Customers);
    lsCustomers = (ListView) findViewById(R.id.list_customers);
    Log.d("","Error1: "); //I am getting error after this line
    lsCustomers.setAdapter(adapter);
    }catch(Exception e){
        Toast.makeText(context, "No record found", Toast.LENGTH_LONG);
    }
    DB.close();

    btnaddcustomers.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, AddCustomers.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void addCustomers(View paramView) {

    Intent intent = new Intent(MainActivity.this, AddCustomers.class);
    startActivity(intent);
}
 }

当我运行应用程序时,我得到例外,

06-27 11:03:55.734: E/AndroidRuntime(11560): FATAL EXCEPTION: main
06-27 11:03:55.734: E/AndroidRuntime(11560): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.measureScrapChild(ListView.java:1170)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.ListView.onMeasure(ListView.java:1162)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.View.measure(View.java:8313)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.os.Looper.loop(Looper.java:130)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invoke(Method.java:507)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-27 11:03:55.734: E/AndroidRuntime(11560):    at dalvik.system.NativeStart.main(Native Method)

请更正我的代码。我在哪里做错了?我对这个例外感到恼火。请说明我收到此错误的原因。

任何帮助表示赞赏!!谢谢!

1 个答案:

答案 0 :(得分:4)

问题是您对Adapter和Activity使用相同的布局。

请参阅,您要在 activity_main.xml listview中设置textview,并在其中设置数据库中的值。

我建议您参考this示例,了解如何从数据库中获取值并在listview中显示。