我一直试图让我的列表视图工作很长一段时间,我只是将我的监听器从onClick更改为onItemClick以更好地处理listview项目但应用程序再次崩溃,我的代码如下,任何帮助将不胜感激。
类别:
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
ListView listView = (ListView) findViewById(R.id.listview1);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Resources res = mContext.getResources();
productTitleArray = res.getStringArray(R.array.titles_product);
for (String productTitle : productTitleArray)
{
if(productTitle.equals("productOne"))
{
Log.d("Msg", "Found");
}
else
{
Log.d("Msg", "Not Found");
}
}
}
});
我以前的帖子:
Compare XML String From Java Class When Item In ListView Is Clicked
logcat的:
03-30 16:40:39.027: E/AndroidRuntime(811): FATAL EXCEPTION: main
03-30 16:40:39.027: E/AndroidRuntime(811): java.lang.NullPointerException
03-30 16:40:39.027: E/AndroidRuntime(811): at com.twostarii.test.ListViewAdapterproduct.getView(ListViewAdapterproduct.java:91)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.makeAndAddView(ListView.java:1831)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.fillDown(ListView.java:674)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.fillFromTop(ListView.java:735)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.ListView.layoutChildren(ListView.java:1652)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.View.layout(View.java:14008)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Handler.handleCallback(Handler.java:725)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
03-30 16:40:39.027: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-30 16:40:39.027: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 16:40:39.027: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:40:39.027: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 16:40:39.027: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 16:40:39.027: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
的活动:
public class MainActivityproduct extends MainActivity {
ListView list;
ListViewAdapterproduct adapter;
EditText editsearch;
String[] position;
String[] productTitles;
String[] productDescriptions;
int[] images={
R.drawable.list_product_one,
R.drawable.list_product_two,
R.drawable.list_product_three,
};
ArrayList<ClassproductList> arraylist = new ArrayList<ClassproductList>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = new String[] { "1", "2", "3"};
Resources res = getResources();
productTitles = res.getStringArray(R.array.titles_product);
productDescriptions = res.getStringArray(R.array.descriptions_product);
list = (ListView) findViewById(R.id.listview1);
list.setAdapter(adapter);
for (int i = 0; i < position.length; i++)
{
ClassproductList wp = new ClassproductList(productTitles[i], productDescriptions[i],images[i]);
arraylist.add(wp);
}
adapter = new ListViewAdapterproduct(this, arraylist);
list.setAdapter(adapter);
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
Context mContext;
中的问题为空
在您的活动集mContext = this;
中,因此mContext将是您将能够使用资源的活动context
这是正确的代码:
因为您已经在适配器中发送了this
,所以需要在那里捕获它
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
ArrayList<ClassproductList> arraylist;
ListView listView = (ListView) findViewById(R.id.listview1);
//add this
public ListViewAdapterProduct (Context mContext,ArrayList<ClassproductList> arraylist)
{
this.mContext = mContext;
this.arraylist = arraylist;
}
答案 1 :(得分:0)
您已声明了mContext,但尚未对其进行初始化。
您可以在适配器的构造函数中执行此操作:
public class ListViewAdapterProduct extends BaseAdapter {
Context mContext;
// the construcor that initializes the context
public ListViewAdapterProduct(Context c){
mContext = c;
}
...
但是,您发布的跟踪信息表明问题出在您的适配器的getView()方法第91行。您也应该检查一下。
ListViewAdapterproduct.getView(ListViewAdapterproduct.java:91)
编辑:
list.setAdapter(adapter);
在这一行中,适配器为null,你没有初始化它...(你设置适配器两次,首先是null)。 在本地定义变量是一种很好的做法,只有在实际需要时才使它们成为类变量。这样就很难忘记初始化。
答案 2 :(得分:0)
已解决!!!
我从onItemClick(复杂地使用自定义适配器)改为onClick(简单地使用自定义适配器)并应用:
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String mytitle=holder.title.getText().toString();
if(mytitle.equals("ProductOne")){
Intent intent1 = new Intent(mContext, ProductClass.ProductOne);
mContext.startActivity(intent1);
...
}
}
不得不补充:
String mytitle=holder.title.getText().toString();