解决方案 如果您想为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()
方法中更新用户界面,因此我在我的主题的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会找到它。
我很困惑,我不知道如何继续。我将不胜感激任何指导和帮助。谢谢。
答案 0 :(得分:0)
我没有在XML中声明onClick,而是在Java文件中声明了它。在我的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之前我必须按两次它们。