我读过Could not find a method onClick(View) in the activity 和java.lang.IllegalStateException: Could not find a method onClick(View)但仍然无法解决这个问题。
我只是尝试学习Android,我尝试使用按钮点击刷新列表..
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/OnlineShopping"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="@string/onlineShopping"
android:textSize="10sp" />
<Button
android:id="@+id/MyAccount"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="@string/myAccount"
android:textSize="10sp" />
<Button
android:id="@+id/CheckIn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:onClick="refresh"
android:text="@string/checkIn"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.45"
android:orientation="vertical" >
<TextView
android:id="@+id/PlacesListLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="3sp"
android:text="@string/retrievingPlaces" />
<ListView
android:id="@+id/PlacesList"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ListView placesList;
private List<Place> places = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placesList = (ListView) findViewById(R.id.PlacesList);
new CheckInTask().execute();
// start retrieving the list of nearby places
new ListOfPlacesAsyncRetriever().execute();
placesList.setOnItemClickListener(placesListClickListener);
}
/**
* AsyncTask for calling Mobile Assistant API for checking into a
* place (e.g., a store).
*/
private class CheckInTask extends AsyncTask<Void, Void, Void> {
/**
* Calls appropriate CloudEndpoint to indicate that user checked into a place.
*
* @param params the place where the user is checking in.
*/
@Override
protected Void doInBackground(Void... params) {
CheckIn checkin = new com.mobileshopping.checkinendpoint.model.CheckIn();
// Set the ID of the store where the user is.
checkin.setPlaceId("StoreNo123");
Checkinendpoint.Builder builder = new Checkinendpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
builder = CloudEndpointUtils.updateBuilder(builder);
Checkinendpoint endpoint = builder.build();
try {
endpoint.insertCheckIn(checkin).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
/**
* AsyncTask for retrieving the list of places (e.g., stores) and updating the
* corresponding results list.
*/
private class ListOfPlacesAsyncRetriever extends AsyncTask<Void, Void, CollectionResponsePlace> {
@Override
protected CollectionResponsePlace doInBackground(Void... params) {
Placeendpoint.Builder endpointBuilder = new Placeendpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
endpointBuilder = CloudEndpointUtils.updateBuilder(endpointBuilder);
CollectionResponsePlace result;
Placeendpoint endpoint = endpointBuilder.build();
try {
result = endpoint.listPlace().execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = null;
}
return result;
}
@Override
@SuppressWarnings("null")
protected void onPostExecute(CollectionResponsePlace result) {
ListAdapter placesListAdapter = createPlaceListAdapter(result.getItems());
placesList.setAdapter(placesListAdapter);
places = result.getItems();
}
public void refresh(View test){
}
private ListAdapter createPlaceListAdapter(List<Place> places) {
final double kilometersInAMile = 1.60934;
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (Place place : places) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("placeIcon", R.drawable.ic_launcher);
map.put("placeName", place.getName());
map.put("placeAddress", place.getAddress());
String distance = "1.2";
map.put("placeDistance", distance);
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.place_item,
new String[] {"placeIcon", "placeName", "placeAddress", "placeDistance"},
new int[] {R.id.place_Icon, R.id.place_name, R.id.place_address, R.id.place_distance});
return adapter;
}
}
private OnItemClickListener placesListClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Place selectedPlace = places.get((int) arg3);
new CheckInTask().execute();
PlaceDetailsActivity.currentPlace = selectedPlace;
Intent i = new Intent(MainActivity.this, PlaceDetailsActivity.class);
startActivity(i);
}
};
}
从那个activity_main.xml我尝试在Activity类上调用函数刷新..但是从logcat我得到了这个:
03-18 03:51:43.615: E/AndroidRuntime(759):
java.lang.IllegalStateException: Could not find a method refresh(View)
in the activity class com.mobileshopping.MainActivity
for onClick handler on view class android.widget.Button with id 'CheckIn'
我是否需要在其他地方注册或者我有一些丢失的代码?
答案 0 :(得分:4)
您的refresh(View)
课程中有ListOfPlacesAsyncRetriever
方法,而不是直接位于MainActivity
课程内。将它移到你的外面,你应该很好。
答案 1 :(得分:2)
您已将refresh()
方法放在Asynctask
内,这就是系统无法找到该方法的原因。但它应该是MainActivity
类的方法。
现在,将refresh()
方法放在AsyncTask
之外,如下所示......
public class MainActivity extends Activity {
private ListView placesList;
private List<Place> places = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placesList = (ListView) findViewById(R.id.PlacesList);
new CheckInTask().execute();
// start retrieving the list of nearby places
new ListOfPlacesAsyncRetriever().execute();
placesList.setOnItemClickListener(placesListClickListener);
}
public void refresh(View test){
}
.............
...........
}