1

正如您在下面的 logcat 中看到的,我得到一个 nullPointer,它指出错误在这一行(来自下面的 onClick 方法): text.setText(cur.getString(0));

LogCat "Seek out a Space Phenomenon" 的第四行是我将 cur.getString(0) 输出到 Log ......所以根据我的说法,没有空指针,但当然我可能错了。谁能告诉我我的方式的错误?

public class QuestsAdapter extends BaseAdapter implements OnClickListener {

    public Activity activity;
    private static LayoutInflater inflater=null;
    Cursor c;
    User player;

    public QuestsAdapter(Activity a, Cursor cur, User u) {
        activity = a;
        player = u;
        c = cur;
        c.moveToFirst();
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return c.getCount();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView title;
        public TextView credits;
        public TextView xp;
        public TextView loot;
        public Button engage;
        //public ProgressBar prog;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.quest_list, null);
            holder=new ViewHolder();
            holder.title=(TextView)vi.findViewById(R.id.questTitle);
            holder.credits=(TextView)vi.findViewById(R.id.questCredits);
            holder.xp=(TextView)vi.findViewById(R.id.questXP);
            holder.loot=(TextView)vi.findViewById(R.id.questLoot);
            holder.engage = (Button) vi.findViewById(R.id.questButton);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        c.moveToPosition(position);
        holder.title.setText(c.getString(1));
        holder.credits.setText(NumberFormat.getInstance().format(c.getInt(2)) + " - " +
                NumberFormat.getInstance().format(c.getInt(3)) + "UC");
        holder.xp.setText("Exp: +" + c.getString(4));
        holder.loot.setText("Loot: " + c.getString(8));
        holder.engage.setId(c.getInt(0));
        holder.engage.setOnClickListener(this);
        return vi;
    }

    @Override
    public void onClick(View v) {
        DBAdapter db = new DBAdapter(activity);
        db.open(false);
        final Cursor cur = db.getQuest(v.getId());
        cur.moveToFirst();
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle(R.string.success);
        dialog.setCancelable(true);
        //there are a lot of settings, for dialog, check them all out!

        //set up text
        TextView text = (TextView) dialog.findViewById(R.id.questTitle);
        Log.i("INFO", cur.getString(0));
        text.setText(cur.getString(0));

        //set up image view
        //ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
        //img.setImageResource(R.drawable.nista_logo);

        //set up button
        Button button = (Button) dialog.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //now that the dialog is set up, it's time to show it    
        dialog.show();
    }
}

接下来我的Logcat:

04-11 07:58:03.753: I/INFO(578): opened db
04-11 07:58:04.023: I/INFO(578): db already exists
04-11 07:58:12.203: D/dalvikvm(578): GC_FOR_MALLOC freed 5170 objects / 286304 bytes in 66ms
04-11 07:58:12.223: I/INFO(578): Seek Out a Space Phenomenon
04-11 07:58:12.223: D/AndroidRuntime(578): Shutting down VM
04-11 07:58:12.233: W/dalvikvm(578): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-11 07:58:12.233: E/AndroidRuntime(578): FATAL EXCEPTION: main
04-11 07:58:12.233: E/AndroidRuntime(578): java.lang.NullPointerException
04-11 07:58:12.233: E/AndroidRuntime(578):  at com.nikopiko.spacelords.QuestsAdapter.onClick(QuestsAdapter.java:96)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.view.View.performClick(View.java:2408)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.view.View$PerformClick.run(View.java:8816)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.os.Handler.handleCallback(Handler.java:587)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.os.Looper.loop(Looper.java:123)
04-11 07:58:12.233: E/AndroidRuntime(578):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 07:58:12.233: E/AndroidRuntime(578):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 07:58:12.233: E/AndroidRuntime(578):  at java.lang.reflect.Method.invoke(Method.java:521)
04-11 07:58:12.233: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 07:58:12.233: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 07:58:12.233: E/AndroidRuntime(578):  at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0