当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Errors in Android project

问题描述:

I'm having problems working my way through an old tutorial, essentially i'm trying to pull the data returned in JSON format from www.amazingjobs.co.uk/app/marc/api/candidates/ into a listview.

I literally started learning android/java 24 hours ago so its likely it may be very simple but I just can't quite seem to figure it out, lines with errors are in bold!

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.HashMap;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.app.ListActivity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

import com.google.gson.Gson;

public class Searchjobs extends Activity {

// url to make request

private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/";

// JSON Node names

private static final String TAG_RESULTS = "results";

// Jobs JSONArray

JSONArray jobs = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_searchjobs);

Button Searchbutton = (Button) findViewById(R.id.Searchbutton);

Searchbutton.setOnClickListener(searchClick);

}

private OnClickListener searchClick = new OnClickListener() {

public void onClick(View v) {

// Hashmap for ListView

ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>();

// Creating JSON Parser instance

JSONParser jParser = new JSONParser();

// getting JSON string from URL

JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/");

// Getting Array of Contacts

try {

jobs = json.getJSONArray(TAG_RESULTS);

for(int i = 0; i < jobs.length();i++){

JSONObject j = jobs.getJSONObject(i);

String title = j.getString("job_title");

HashMap<String, String> map = new HashMap<String, String>();

map.put("Title", title);

JobList.add(map);

}

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

**ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item,

new String[] { "Title" }, new int[] {R.id.title});**

**ERROR: The constructor SimpleAdapter(new View.OnClickListener(){}, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined**

**setListAdapter(adapter);

The method setListAdapter(ListAdapter) is undefined for the type new View.OnClickListener(){}**

// selecting single ListView item

**ListView lv = getListView();**

**ERROR: The method getListView() is undefined for the type new View.OnClickListener(){}**

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

}

});

};

};

EDIT Ok new code is below, errors are, which seems odd because the activity errors relate to an activity that definately exists, have tried restart/clean in eclipse too.

Description Resource Path Location Type

activity_searchjobs cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 47 Java Problem

activity_searchjobs cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 105 Java Problem

Searchbutton cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 48 Java Problem

list_item cannot be resolved or is not a field Searchjobs.java /AmazingJobs/src/app/android/amazingjobs line 83 Java Problem

public class Searchjobs extends ListActivity {

// url to make request

private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/";

// JSON Node names

private static final String TAG_RESULTS = "results";

// Jobs JSONArray

JSONArray jobs = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_searchjobs);

Button Searchbutton = (Button) findViewById(R.id.Searchbutton);

Searchbutton.setOnClickListener(searchClick);

}

private OnClickListener searchClick = new OnClickListener() {

public void onClick(View v) {

// Hashmap for ListView

ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>();

// Creating JSON Parser instance

JSONParser jParser = new JSONParser();

// getting JSON string from URL

JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/");

// Getting Array of Contacts

try {

jobs = json.getJSONArray(TAG_RESULTS);

for(int i = 0; i < jobs.length();i++){

JSONObject j = jobs.getJSONObject(i);

String title = j.getString("job_title");

HashMap<String, String> map = new HashMap<String, String>();

map.put("Title", title);

JobList.add(map);

}

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ListAdapter adapter = new SimpleAdapter(getApplication(), JobList,R.layout.list_item,

new String[] { "Title" }, new int[] {R.id.title});

setListAdapter(adapter);

// selecting single ListView item

ListView lv = getListView();

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

}

});

};

};

网友答案:
ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item,
                        new String[] { "Title" }, new int[] {R.id.title});

should be changed to:

ListAdapter adapter = new SimpleAdapter(getApplicationContext(), JobList,R.layout.list_item,
                        new String[] { "Title" }, new int[] {R.id.title});

Second,

ListView lv = getListView();

will be resolved if you extend ListActivity instead of Activity.

网友答案:

You need to extend ListActivity not Activity to make methods like getListView() avaialable or just replace it with findViewById() if you do not want to use ListActivity (it's not mandatory).

分享给朋友:
您可能感兴趣的文章:
随机阅读: