我正在尝试从自定义 lisview 启动一项活动。我已经隐藏了 onItemclick 方法并通过创建新意图调用了该类!
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent i = new Intent(getApplicationContext(), InstalledApps.class);
startActivity(i);
}
}
但是当我尝试启动它时,它给了我一个空指针异常。我想要做的是,获取单击项目的位置并通过 if 子句将其引导到正确的活动。以下是日志猫输出。
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;
}
}