我正在尝试从自定义lisview启动活动。我已经通过创建一个新意图来编写onItemclick方法nd,称为类!
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent i = new Intent(getApplicationContext(), InstalledApps.class);
startActivity(i);
}
}
但是当我尝试启动它时,它会给我一个nullpointer异常。我要做的是,获取被点击项目的位置,并通过if子句将其指向正确的活动。以下是log cat输出。
07-05 04:39:52.718: W/dalvikvm(3189): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-05 04:39:52.758: E/AndroidRuntime(3189): FATAL EXCEPTION: main
07-05 04:39:52.758: E/AndroidRuntime(3189): java.lang.NullPointerException
07-05 04:39:52.758: E/AndroidRuntime(3189): at com.sliit.droidman.thirdpartyapplist.ListViewAdapter.getView(ListViewAdapter.java:69)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.ListView.onMeasure(ListView.java:1156)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.View.measure(View.java:15172)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.View.measure(View.java:15172)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.View.measure(View.java:15172)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.View.measure(View.java:15172)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-05 04:39:52.758: E/AndroidRuntime(3189): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.View.measure(View.java:15172)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.os.Handler.handleCallback(Handler.java:615)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.os.Looper.loop(Looper.java:137)
07-05 04:39:52.758: E/AndroidRuntime(3189): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-05 04:39:52.758: E/AndroidRuntime(3189): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 04:39:52.758: E/AndroidRuntime(3189): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 04:39:52.758: E/AndroidRuntime(3189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-05 04:39:52.758: E/AndroidRuntime(3189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-05 04:39:52.758: E/AndroidRuntime(3189): at dalvik.system.NativeStart.main(Native Method)
请让我知道我在这里做错了什么!
适配器类。
package com.sliit.droidman.thirdpartyapplist;
import com.sliit.droidman.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter
{
Activity context;
String title[];
String description[];
Drawable icon[];
public ListViewAdapter(Activity context, String[] title, String[] description , Drawable[] ico) {
super();
this.context = context;
this.title = title;
this.description = description;
this.icon = ico;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.appname);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.packagename);
holder.image = (ImageView) convertView.findViewById(R.id.avatar);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.image.setImageDrawable(icon[position]);
return convertView;
}
}
答案 0 :(得分:1)
07-05 04:39:52.758: E/AndroidRuntime(3189): at com.sliit.droidman.thirdpartyapplist.ListViewAdapter.getView(ListViewAdapter.java:69)
com.sliit.droidman.thirdpartyapplist.ListViewAdapter
的{{1}}方法的第69行有一个空指针。找出导致该空指针的原因并确保它不会发生。
看来第69行是以下
getView
可能导致这种情况的一些事情:
holder.txtViewTitle.setText(title[position]);
参数太大,你会得到一个ArrayIndexOutOfBoundsException。position
个实例的txtViewTitle
成员为holder
,可能是因为第59行的身份null
不正确。您可以通过在调试器的第69行放置断点并检查R.id.appname
和holder.txtViewTitle
的值来解决这个问题。
答案 1 :(得分:0)
试试这个:
package com.sliit.droidman.thirdpartyapplist;
import com.sliit.droidman.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter
{
Activity context;
String title[];
String description[];
Drawable icon[];
public ListViewAdapter(Activity context, String[] title, String[] description , Drawable[] ico) {
super();
this.context = context;
this.title = title;
this.description = description;
this.icon = ico;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//You have to overide the getView method
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//added row, to convert view
View row = convertView;
ViewHolder holder;
if (convertView == null)
{
//added casting to the context
LayoutInflater inflater = ((Activity)this.context).getLayoutInflater();
//made changes here
row = inflater.inflate(R.layout.listitem_row, parent, false);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.appname);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.packagename);
holder.image = (ImageView) convertView.findViewById(R.id.avatar);
row.setTag(holder);
}
else
{ //added row
holder = (ViewHolder) row.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.image.setImageDrawable(icon[position]);
//changed to row
return row;
}
}
static class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
ImageView image;
}
基本上,我在listadapter的类之外创建了一个静态ViewHolder类,并做了一些其他的小改动,包括覆盖你的getView方法。我留下了我做出改动的评论。让我知道这个是否奏效!希望这可以帮助!