我需要一个严肃的帮助,我的应用程序工作正常,直到现在。我正在使用sqLite
数据库进行项目并显示搜索结果。我使用TextView
来显示搜索结果,它工作正常,但当我使用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 :(得分:1)
列表视图不应位于scrollview
中。
发布日志可以查看错误的来源。
编辑1
我检查了你的getDetails
方法,你定义了一个包含100个索引的数组,如果结果超过100,该怎么办?你应该使用ArrayList
或者用{{1的数量来定义数组}}
cursor
编辑2
错误似乎来自
String[] result = new String[c.getCount()];
检查 String CustomerNam = CustomerName.getText().toString().trim();
是否为空
答案 1 :(得分:0)
根据我对ListView的经验,我从来没有在ScrollView中使用它。
它应该自己滚动。