1

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

这是我的代码

// 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();
            }
        });

在这里记录猫显示

    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)

感谢帮助!

4

5 回答 5

1

之一

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

一片空白。

  • 观点:没有意义
  • findViewById(R.id.report_id)) :您确定该视图存在于项目布局中吗?
  • getText() :report_id 视图的文本可以为空
于 2012-09-25T08:50:05.133 回答
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 的索引传递。

于 2012-09-25T11:29:32.480 回答
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();

于 2012-09-25T08:50:17.177 回答
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);
            }
于 2012-09-26T00:40:44.770 回答
0

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

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

将 'tv' 附加到其 ID 后,尝试从中获取文本,使用

字符串 str = tv.getText().toString();

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

于 2012-09-25T09:04:08.153 回答