ListView onItemClick:NullPointerException检索视图

时间:2012-09-25 08:47:04

标签: android

我无法点击列表视图来传递新活动。请指教我如何解决这个问题。

这里是我的代码

// set UI
        setContentView(R.layout.show_permission);

        //get list from background
        show = getListView();

        //object for permission list
        permission_list = new ArrayList<HashMap<String,String>>();

        // get uid from main activity
        Intent i = getIntent();
        uid = i.getStringExtra(TAG_UID);

        // get data from background thread
        new GetPermission().execute();



        show.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                //getting item from list Item
                String project_id = ((TextView) view.findViewById(R.id.report_id)).getText().toString(); <---error here
                String project_name = ((TextView) view.findViewById(R.id.pro_name)).getText().toString();

                Intent in = getIntent();
                //send report_id to next activity
                in.putExtra(TAG_PROJECT_ID, project_id);
                in.putExtra(TAG_PROJECT_NAME, project_name);
                in.putExtra(TAG_UID, uid);
                setResult(100, in);

                finish();
            }
        });

并在这里登录cat show

    09-25 15:41:27.510: E/AndroidRuntime(1457): FATAL EXCEPTION: main
09-25 15:41:27.510: E/AndroidRuntime(1457): java.lang.NullPointerException
09-25 15:41:27.510: E/AndroidRuntime(1457):     at com.example.androidhive.ProjectPermission$1.onItemClick(ProjectPermission.java:82)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.widget.AbsListView$1.run(AbsListView.java:3529)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.os.Handler.handleCallback(Handler.java:615)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.os.Looper.loop(Looper.java:137)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invokeNative(Native Method)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invoke(Method.java:511)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 15:41:27.510: E/AndroidRuntime(1457):     at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助!

5 个答案:

答案 0 :(得分:1)

之一
((TextView) view.findViewById(R.id.report_id)).getText().toString();

为空。

  • 观点:没有意义
  • findViewById(R.id.report_id)):您确定项目布局中存在此视图吗?
  • getText():report_id视图的文本可以为null

答案 1 :(得分:0)

而不是使用

String project_id = ((TextView) view.findViewById(R.id.report_id)).getText().toString();

试试这个:

<强> mTextView = (TextView)findViewById(R.id.report_id);

<强> String project_id = mTextView.getText().toString();

答案 2 :(得分:0)

创建一个textview变量说tv,然后

tv =(TextView)findViewById(R.id.report_id);

将'tv'附加到其ID后,尝试使用

从中获取文本

String str = tv.getText()。toString();

在'Toast'中显示字符串'str',以确定它是否不包含空值,如果这不起作用,请在此处发布代码。

答案 3 :(得分:0)

尝试使用以下方法:

  String project_id = ((TextView) view.getChildAt(index of project id view)).getText().toString(); 

  String project_name = ((TextView) view.getChildAt(index of project name view)).getText().toString();

例如,如果项目ID文本视图是列表项视图中的第一个视图,则将0作为项目ID的索引传递。

答案 4 :(得分:0)

感谢所有回复,我找到了解决方案来解决这个问题

setContentView(R.layout.show_permission);

        ListView lv = getListView();

        Intent i = getIntent();
        uid = i.getStringExtra(TAG_UID);


        //report list
        permission_list = new ArrayList<HashMap<String,String>>();
        //load report in background
        new GetPermission().execute();

        // listView OnClicklistener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                // TODO Auto-generated method stub
                //getting item from list Item
                String project_name = ((TextView) view.findViewById(R.id.pro_name)).getText().toString();

                // pass data to edit
                Intent edit = new Intent(getApplicationContext(), TimeReport.class);

                //send report_id to next activity
                edit.putExtra(TAG_PROJECT_NAME, project_name);
                edit.putExtra(TAG_UID, uid);

                startActivityForResult(edit, 100);
            }