方法startActivityForResult(Intent,int)未定义类型new AdapterView.OnItemClickListener

时间:2011-02-13 15:31:33

标签: android listview android-activity android-intent android-asynctask

哎, 我是Android的新手,我正在尝试编写一个从网上获取json数据的应用程序,创建一个列表。如果单击列表项,则会启动一个新活动,其中显示活动的详细信息。但是我似乎无法启动第二个活动,我不断收到错误“方法startActivityForResult(Intent,int)未定义为eclipse中的新类型AdapterView.OnItemClickListener(){}”。这是完整的来源。 任何想法?

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new LongOperation(this).execute();
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        new LongOperation(this).execute();
        return true;
    }
}

class LongOperation extends AsyncTask<String, Void, String> {
    private Main longOperationContext = null;
    ProgressDialog pd = null;
    TextView tv = null;
    ListView lv1 = null;
    String [] lv_arr;
    String [] lv_arr_id;
    private static final int ACTIVITY_CREATE = 0;



    public LongOperation(Main context) {
        longOperationContext = context;
        Log.v("LongOper", "Konstuktor");
    }

    @Override
    protected String doInBackground(String... params) {
        Log.v("doInBackground", "inside");
        try {
            URL json = new URL("http://www.ytmusicplayer.com/jsontest.php");
            URLConnection tc = json.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                    JSONArray ja = new JSONArray(line);
                    Log.v("line = ", " " + ja.length());
                    lv_arr = new String[ja.length()];
                    lv_arr_id = new String[ja.length()];
                    for (int i=0;i<ja.length();i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        lv_arr[i] = jo.getString("name");
                        lv_arr_id[i] = jo.getString("id");
                    }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.v("Error", "URL exc");
        } catch (IOException e) {
            e.printStackTrace();
            Log.v("ERROR", "IOEXECPTOIn");
        } catch (JSONException e) {
            e.printStackTrace();
            Log.v("Error", "JsonException");
        }
        Log.v("Line: ", lv_arr[0] + " - " + lv_arr[1]);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        lv1.setAdapter(new ArrayAdapter<String>(longOperationContext,android.R.layout.simple_list_item_1 , lv_arr));
        lv1.setTextFilterEnabled(true);
        lv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                        Intent i = new Intent(longOperationContext, Details.class);
                        i.putExtra("id", lv_arr_id[position]);
                        startActivityForResult(i, ACTIVITY_CREATE);
                }
        });
        pd.hide();
    }
    protected void onPreExecute() {
        lv1 = (ListView)longOperationContext.findViewById(R.id.ListView01);
        pd = new ProgressDialog(longOperationContext);
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setMessage("Loading...");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

提前致谢

1 个答案:

答案 0 :(得分:4)

这是因为该行代码出现在内部类中。 Java认为你正在尝试访问内部类的方法,当你想要的是外部类的方法(Activity的方法)。在任何方法调用之前都有一个隐式this关键字,因此您实际上正在执行this.startActivityForResult(i, ACTIVITY_CREATE);

尝试使用Main.this.startActivityForResult(i, ACTIVITY_CREATE);

此语法是访问外部类类型的对象的方法。