解
如果要为AutoCompleteTextView使用onClickListener,请不要在XML中进行操作,而应在Java文件中进行操作。请参阅下面的答案。
原始问题
抱歉,如果我错过了一些简单的事情,但是在过去2个小时中我一直在努力工作,但我遇到了麻烦。
在我的活动中,我有4个AutoCompleteTextViews,它们由ArrayList填充,其值来自数据库查询。一切正常,直到在四个AutoCompleteTextViews的任何一个上添加android:onClick
属性。我不知道发生了什么,清理项目并没有拯救我。
以下是相关代码:
我用于AutoCompleteTextViews的全局变量:
private ArrayList<String> brands = new ArrayList<String>();
private ArrayList<String> items = new ArrayList<String>();
private ArrayList<String> parts = new ArrayList<String>();
private ArrayList<String> barcodes = new ArrayList<String>();
private AutoCompleteTextView brandACTV;
private AutoCompleteTextView itemACTV;
private AutoCompleteTextView partACTV;
private AutoCompleteTextView barcodeACTV;
private ArrayAdapter<String> adapter;
我的
onCreate()
函数,非常标准,我设置布局并初始化AutoCompleteTextViews@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
TextView customerName = (TextView) findViewById(R.id.customerName);
customerName.setText(getIntent().getStringExtra("customer"));
brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand);
itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName);
partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart);
barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode);
new InitTask().execute("");
}
执行
InitTask
线程时发生的情况是,我在执行数据库查询以为AutoCompleteTextViews初始化ArrayList数组时显示旋转进度条。在InitTask的
doInBackground
方法中,我调用getArrays()
函数:public void getArrays(){
//itemListDB is an object of my DatabaseAdapter class
itemListDB.open();
brands = itemListDB.getAllBrands();
items = itemListDB.getAllItemNames();
parts = itemListDB.getAllParts();
barcodes = itemListDB.getAllBarcodes();
itemListDB.close();
}
而且由于无法在
doInBackground()
方法中更新UI,因此我在线程的setAutoCompeteTextViews()
方法中调用了onPostExecute()
方法:public void setAutoCompeteTextViews(){
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, brands);
brandACTV.setAdapter(adapter);
brandACTV.setThreshold(1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);
itemACTV.setAdapter(adapter);
itemACTV.setThreshold(1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, parts);
partACTV.setAdapter(adapter);
partACTV.setThreshold(1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, barcodes);
barcodeACTV.setAdapter(adapter);
barcodeACTV.setThreshold(1);
}
对于我的XML,我将仅展示如何编写一个AutoCompleteTextView,因为它们都很相似,只是
id
有所不同。<AutoCompleteTextView
android:id="@+id/actvBrand"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_above="@+id/price"
android:layout_alignLeft="@+id/submit"
android:ems="10"
android:hint="Brand"
android:onClick="actvClicked" />
然后在活动中定义
actvClicked()
函数,如下所示:public void actvClicked(View view){
switch (view.getId()) {
case R.id.actvBrand:
Log.d("Hi", "Brand pressed");
break;
case R.id.actvItemName:
Log.d("Hi", "Item name pressed");
break;
case R.id.actvPart:
Log.d("Hi", "Part pressed");
break;
case R.id.actvBarcode:
Log.d("Hi", "Barcode pressed");
break;
}
}
但是,它不起作用,logCat给了我这个错误:
04-25 15:40:28.270: E/AndroidRuntime(24104): FATAL EXCEPTION: main
04-25 15:40:28.270: E/AndroidRuntime(24104): Process: com.agict.marswin, PID: 24104
04-25 15:40:28.270: E/AndroidRuntime(24104): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.agict.marswin/com.agict.marswin.SearchPage}: android.view.InflateException: Binary XML file line #73: Error inflating class android.widget.AutoCompleteTextView
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.os.Looper.loop(Looper.java:136)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 15:40:28.270: E/AndroidRuntime(24104): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:40:28.270: E/AndroidRuntime(24104): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 15:40:28.270: E/AndroidRuntime(24104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 15:40:28.270: E/AndroidRuntime(24104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 15:40:28.270: E/AndroidRuntime(24104): at dalvik.system.NativeStart.main(Native Method)
04-25 15:40:28.270: E/AndroidRuntime(24104): Caused by: android.view.InflateException: Binary XML file line #73: Error inflating class android.widget.AutoCompleteTextView
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-25 15:40:28.270: E/AndroidRuntime(24104): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-25 15:40:28.270: E/AndroidRuntime(24104): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.Activity.setContentView(Activity.java:1929)
04-25 15:40:28.270: E/AndroidRuntime(24104): at com.agict.marswin.SearchPage.onCreate(SearchPage.java:39)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.Activity.performCreate(Activity.java:5231)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-25 15:40:28.270: E/AndroidRuntime(24104): ... 11 more
04-25 15:40:28.270: E/AndroidRuntime(24104): Caused by: java.lang.reflect.InvocationTargetException
04-25 15:40:28.270: E/AndroidRuntime(24104): at java.lang.reflect.Constructor.constructNative(Native Method)
04-25 15:40:28.270: E/AndroidRuntime(24104): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-25 15:40:28.270: E/AndroidRuntime(24104): ... 24 more
04-25 15:40:28.270: E/AndroidRuntime(24104): Caused by: java.lang.NullPointerException
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.AutoCompleteTextView$PassThroughClickListener.access$302(AutoCompleteTextView.java:1242)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.AutoCompleteTextView.setOnClickListener(AutoCompleteTextView.java:200)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.view.View.<init>(View.java:3797)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.TextView.<init>(TextView.java:623)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.EditText.<init>(EditText.java:60)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:137)
04-25 15:40:28.270: E/AndroidRuntime(24104): at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:133)
04-25 15:40:28.270: E/AndroidRuntime(24104): ... 27 more
而
com.agict.marswin.SearchPage.onCreate(SearchPage.java:39)
指向我的setContentView()
函数中的onCreate()
调用的位置。很抱歉,但是我找不到任何错误,因为我正确地声明了该函数,并且Java文件中没有XML需要的内容。
我很困惑,我不知道如何进行。我将不胜感激任何指导和帮助。谢谢。
最佳答案
我没有在XML中声明onClick,而是在Java File中声明了它。在我的onCreate()
中,我这样做:
brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand);
itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName);
partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart);
barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode);
brandACTV.setOnClickListener(actvClicked);
itemACTV.setOnClickListener(actvClicked);
partACTV.setOnClickListener(actvClicked);
barcodeACTV.setOnClickListener(actvClicked);
我声明了一个名为actvClicked的OnClickListener函数,如下所示:
OnClickListener actvClicked = new OnClickListener(){
@Override
public void onClick(View view){
switch (view.getId()) {
case R.id.actvBrand:
Log.d("Hi", "Brand pressed");
break;
case R.id.actvItemName:
Log.d("Hi", "Item name pressed");
break;
case R.id.actvPart:
Log.d("Hi", "Part pressed");
break;
case R.id.actvBarcode:
Log.d("Hi", "Barcode pressed");
break;
}
}
};
但是,我现在的问题是,在触发onClickListener之前,我必须按两次它们中的任何一个。