2017-07-15 136 views
-1

我需要一個嚴重的幫助,我的應用程序工作正常,直到現在。我在做一個使用sqLite數據庫並顯示搜索結果的項目。我用TextView顯示搜索結果,它工作正常,但當我使用ListView來顯示它時崩潰,當我滾動它。需要幫助.......當我滾動我的ListView我的應用程序崩潰

我在 SearchCustomer.java

public class SearchCustomer extends Activity { 


Button searchCustomer; 
String errormsg = "No Result Found"; 
String[] nameCus; 
ListView Cusinfo; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_customer); 

    searchCustomer = (Button) findViewById(R.id.Bsearch); 

    Cusinfo = (ListView) findViewById(R.id.cusinfoList); 
    final AutoCompleteTextView CustomerName = (AutoCompleteTextView) findViewById(R.id.CustomerName); 

    final Customer name = new Customer(this); 
    searchCustomer.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      String CustomerNam = CustomerName.getText().toString().trim(); 
      name.open(); 
      nameCus = name.getDetails(CustomerNam); 
      name.close(); 
      ListAdapter cus = new ArrayAdapter<>(SearchCustomer.this, android.R.layout.simple_list_item_1, nameCus); 
      Cusinfo.setAdapter(cus); 
      Cusinfo.setOnItemClickListener(
        new AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
          String det = String.valueOf(parent.getItemAtPosition(position)); 
          Toast.makeText(SearchCustomer.this, det, Toast.LENGTH_LONG).show(); 
         } 
        }); 
     }else{ 
      Toast.makeText(SearchCustomer.this, errormsg, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
} 
代碼

我在Customer.java

public class Customer { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "cus_name"; 
public static final String KEY_PHONE = "cus_phone"; 
public static final String KEY_ADDRESS = "cus_address"; 
public static final String KEY_DATE = "cus_date"; 
public static final String KEY_DETAILS = "cus_details"; 
public static final String KEY_AMOUNT = "cus_amount"; 
public static final String KEY_SRI = "cus_sri"; 

private static final String DATABASE_NAME = "Customerdb"; 
private static final String DATABASE_TABLE = "customerTable"; 
private static final int DATABASE_VERSION = 1; 
private final Context ourContext; 
private DbHelper ourHelper; 
private SQLiteDatabase ourDatabase; 


public String[] getDetails(String customerNam) { 
    String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_PHONE, KEY_ADDRESS, KEY_DATE, KEY_DETAILS, KEY_AMOUNT, KEY_SRI}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_PHONE + "=?", 
      new String[]{customerNam}, null, null, null, null); 
    String[] result = new String[100]; 
    int i = 0; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iPhone = c.getColumnIndex(KEY_PHONE); 
    int iAddress = c.getColumnIndex(KEY_ADDRESS); 
    int iDate = c.getColumnIndex(KEY_DATE); 
    int iDetails = c.getColumnIndex(KEY_DETAILS); 
    int iAmount = c.getColumnIndex(KEY_AMOUNT); 
    int iSri = c.getColumnIndex(KEY_SRI); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     result[i] = c.getString(iRow) + "\n" + "Name: " + c.getString(iName) + "\n" + 
       "PhoneNo.: " + c.getString(iPhone) + "\n" + "Address: " + c.getString(iAddress) + "\n" + 
       "Date: " + c.getString(iDate) + "\n" + "Details: " + c.getString(iDetails) + "\n" + 
       "Amount: Rs." + c.getString(iAmount) + "\n" + "Type : " + c.getString(iSri) + "\n" + "____________________________________" + "\n"; 
     i++; 
    } 
    return result; 
} 

}

我的代碼代碼activity_search_customer.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="8dp" 
    android:orientation="vertical" 
    tools:context="com.innovation.fae.camerawork.SearchCustomer"> 


    <TextView 
     android:id="@+id/textView" 
     style="@style/AlertDialog.AppCompat" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dp" 
     android:text="@string/enter_the_customer_name" 
     android:textSize="18dp" /> 

    <AutoCompleteTextView 
     android:id="@+id/CustomerName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Customer Phone No." 
     android:inputType="phone" 
     android:paddingTop="20dp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp"> 

     <Button 
      android:id="@+id/Bsearch" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/search" 
      android:textSize="18sp" 
      android:textStyle="italic" /> 


    </LinearLayout> 

     <ListView 
      android:id="@+id/cusinfoList" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#c3deff" 
      android:textColor="#001e71" 
      android:textSize="20sp" /> 

</LinearLayout> 

**Logcat**

07-15 22:38:19.825 29729-29729/com.innovation.fae.camerawork E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.innovation.fae.camerawork, PID: 29729 
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
                       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
                       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
                       at android.widget.AbsListView.obtainView(AbsListView.java:2349) 
                       at android.widget.ListView.makeAndAddView(ListView.java:1864) 
                       at android.widget.ListView.fillDown(ListView.java:698) 
                       at android.widget.ListView.fillGap(ListView.java:662) 
                       at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5007) 
                       at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3424) 
                       at android.widget.AbsListView.startScrollIfNeeded(AbsListView.java:3352) 
                       at android.widget.AbsListView.onTouchMove(AbsListView.java:3780) 
                       at android.widget.AbsListView.onTouchEvent(AbsListView.java:3638) 
                       at android.view.View.dispatchTouchEvent(View.java:8480) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406) 
                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107) 
                       at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2625) 
                       at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1770) 
                       at android.app.Activity.dispatchTouchEvent(Activity.java:2742) 
                       at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2586) 
                       at android.view.View.dispatchPointerEvent(View.java:8675) 
                       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129) 
                       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995) 
                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) 
                       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603) 
                       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569) 
                       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3686) 
                       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577) 
                       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3743) 
                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) 
                       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603) 
                       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569) 
                       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577) 
                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550) 
                       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813) 
                       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787) 
                       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758) 
                       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903) 
                       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 
                       at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) 
                       at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176) 
                      at android.view.ViewRootImpl.doConsumeBatchedI 
+0

需要更多的信息。添加logcat –

回答

1
  1. 列表視圖不應位於scrollview

  2. 發佈日誌可以查看錯誤來自何處。

編輯1
我檢查你的getDetails方法,你定義100個指數的數組,如果結果如果超過100,你應該使用的是一個ArrayList或與統計定義數組什麼從cursor

String[] result = new String[c.getCount()]; 

編輯2
錯誤似乎是從

01來看到
String CustomerNam = CustomerName.getText().toString().trim(); 

檢查CustomerName不爲空

+0

當Listview處於Scrollview時,應用程序不會崩潰,但 –

+0

已生成簽名應用程序,我的手機然後安裝,所以我不能檢查錯誤 –

+1

cricket_007我只是告訴他一個最佳做法 @AravindEkan你可以卸載簽名的一個,並再次調試嗎? – Collins

0
在我與ListView的經驗

,我從來沒有成爲一個滾動型的內部。

它應該自己滾動。

+0

當Listview處於Scrollview時,應用程序不會崩潰,儘管 –

+0

我認爲我在getDetails函數中有問題,請檢查 –

相關問題