简体   繁体   English

错误单击按钮上单击Android的方法

[英]Error calling method on Button click Android

i read Could not find a method onClick(View) in the activity and java.lang.IllegalStateException: Could not find a method onClick(View) but still cant solve this.. 我阅读了在活动java.lang.IllegalStateException中找不到方法onClick(View):找不到方法onClick(View)但仍然无法解决此问题。

I just try to learning Android, and i try to refreshing a list with a button click.. 我只是尝试学习Android,然后尝试通过单击按钮刷新列表。

activity_main.xml 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 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);
      }
  };  


}

from that activity_main.xml i try to call function refresh on Activity class.. but from logcat i got this : 从那个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'

Do i need to register somewhere else or i have some missing code?? 我需要在其他地方注册还是缺少一些代码?

You have the refresh(View) method inside of your ListOfPlacesAsyncRetriever class, instead of directly inside of your MainActivity class. 您在ListOfPlacesAsyncRetriever类内部而不是直接在MainActivity类内部拥有refresh(View)方法。 Move it outside of that and you should be good. 将其移出该范围,您应该会很好。

You have placed the refresh() method inside the Asynctask that's why the system can't find the method. 您已将refresh()方法放在Asynctask ,这就是系统找不到该方法的原因。 But it should be a method of MainActivity class. 但这应该是MainActivity类的方法。

Now, place your refresh() method outside the AsyncTask as below... 现在,将您的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){

    }


   .............

   ...........
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM