当数据库为空时,App force将关闭

时间:2012-02-03 13:24:54

标签: android

我为我的列表创建了一个自定义光标适配器。该列表从数据库中获取一些数据

我的问题是当我尝试进入我的活动并且数据库中没有要显示的项目时,我的应用程序强制关闭。如果我的数据库中没有条目并且没有崩溃,我想获取一个Toast消息应用

这是我的logCat:

02-03 15:21:05.710: E/AndroidRuntime(24768): FATAL EXCEPTION: main
02-03 15:21:05.710: E/AndroidRuntime(24768): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.ListView.clearRecycledState(ListView.java:513)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.ListView.resetList(ListView.java:500)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.ListView.layoutChildren(ListView.java:1534)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.AbsListView.onLayout(AbsListView.java:1863)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.View.layout(View.java:11278)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.View.layout(View.java:11278)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.View.layout(View.java:11278)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.View.layout(View.java:11278)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.View.layout(View.java:11278)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.os.Looper.loop(Looper.java:137)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at android.app.ActivityThread.main(ActivityThread.java:4424)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
02-03 15:21:05.710: E/AndroidRuntime(24768):    at dalvik.system.NativeStart.main(Native Method)
02-03 15:21:07.007: I/Process(24768): Sending signal. PID: 24768 SIG: 9

CODE:

public class Adapter extends CursorAdapter{    
     Cursor c;
     LayoutInflater inflater;
     Context context;

        public Adapter(Context context, Cursor c) {
            super(context, c);    
            this.c = c;
            this.context = context;
            inflater = LayoutInflater.from(context);
        }           
        @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = null;
         try{           
             v = inflater.inflate(R.layout.row, null);}             
             catch (Exception e) {
             e.printStackTrace();
            }
            return v;
        }
            @Override
              public void bindView(View view, final Context context, final Cursor cursor) {    
              final TextView txtName = (TextView) view.findViewById(R.id.textView01);
              txtName.setText(cursor.getString(cursor.getColumnIndex(DBHelper.NAME)));              
              ImageButton button = (ImageButton) view.findViewById(R.id.btn);
              button.setOnClickListener(new OnClickListener() {  

              public void onClick(View arg0) {    
                  Toast.makeText(context,"item Deleted", Toast.LENGTH_SHORT).show();                                   
           }
           });            
        }          
    }

RR.layout.row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="wrap_content"
   android:background="#ffffff" android:gravity="center_vertical"
   android:orientation="horizontal">    

    <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:orientation="vertical"
         android:layout_weight="1"  >
    <TextView 
         android:id="@+id/textView01" android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:textColor="#000000"
         android:textSize="16dp" android:textStyle="bold"  />    
    <ImageButton 
         android:id="@+id/btn"
         android:layout_width="wrap_content" android:layout_height="wrap_content"
         android:background="@drawable/btn_bg" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

你输错了类型,因为你的LayoutParameters应该是AbsListView.LayoutParams 因为它是在您的例外中写的:

ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams