对于新的AdapterView.OnItemClickListener类型,该方法未定义starticvityforresult (Intent, int)。

时间:2022-10-25 20:59:32

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?

嘿,我是android的新手,我正在尝试编写一个应用程序,从web上获取json数据,创建一个列表。如果您点击一个列表项,一个新的活动开始,它显示了活动的详细信息。然而,我似乎不能启动第二个activty,我一直得到错误的“方法starticvityforresult (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) {
    }
}

thanks in advance

谢谢提前

1 个解决方案

#1


4  

This happens because that line of code appears inside an inner class. Java thinks you are trying to access the inner classes's methods, when what you want is the outer classes's methods (the Activity's methods). There is an implicit this keyword before any method call, so you are actually doing this.startActivityForResult(i, ACTIVITY_CREATE);

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

Try using Main.this.startActivityForResult(i, ACTIVITY_CREATE);

试着用Main.this。startActivityForResult(我ACTIVITY_CREATE);

This syntax is how you access the object of the outer class type.

该语法是如何访问外部类类型的对象的。

#1


4  

This happens because that line of code appears inside an inner class. Java thinks you are trying to access the inner classes's methods, when what you want is the outer classes's methods (the Activity's methods). There is an implicit this keyword before any method call, so you are actually doing this.startActivityForResult(i, ACTIVITY_CREATE);

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

Try using Main.this.startActivityForResult(i, ACTIVITY_CREATE);

试着用Main.this。startActivityForResult(我ACTIVITY_CREATE);

This syntax is how you access the object of the outer class type.

该语法是如何访问外部类类型的对象的。