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 sour开发者_JAVA百科ce. 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) {
}
}
thanks in advance
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);
Try using Main.this.startActivityForResult(i, ACTIVITY_CREATE);
This syntax is how you access the object of the outer class type.
精彩评论