English 中文(简体)
方法startActivityForResult(Intent,int)对于类型new AdapterView.OnItemClickListener未定义
原标题:The method startActivityForResult(Intent, int) is undefined for the type new AdapterView.OnItemClickListener

hey, i m new to android and i m trying to code an app which fetches json data off the web, creates a list. if you click on a list item a new activity starts which shows the details of the activity. however i cant seem to start the second activty, i keep getting the error "The method startActivityForResult(Intent, int) is undefined for the type new AdapterView.OnItemClickListener(){}" in eclipse. here is the full source. any ideas?

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) {
    }
}

提前感谢

最佳回答

发生这种情况是因为这行代码出现在内部类中。Java认为您正在尝试访问内部类的方法,而您想要的是外部类的方法(活动的方法)。在任何方法调用之前都有一个隐含的this关键字,所以您实际上是在执行this。startActivityForResult(i,ACTIVITY_CREATE)

尝试使用Mainthis.startActivityForResult(i,ACTIVITY_CREATE)

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

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...