0

我正在尝试从自定义 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;  
}  

 }  
4

2 回答 2

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在'方法的第69 行有一个空指针getView。找出导致该空指针的原因并确保它不会发生。

看来第69行如下

    holder.txtViewTitle.setText(title[position]);  

可能导致这种情况的一些事情:

  • 也许标题数组为空。我认为如果只是position参数太大,你会得到一个 ArrayIndexOutOfBoundsException 。
  • 实例的txtViewTitle成员是,可能是因为第 59 行中的 id不正确。holdernullR.id.appname

您可以通过在调试器的第 69 行放置一个断点并检查 and 的值来解决这个holder.txtViewTitle问题title

于 2012-07-05T20:08:30.680 回答
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 方法。我在我做出更改的地方留下了评论。让我知道这个是否奏效!希望这可以帮助!

于 2012-07-05T21:45:46.853 回答