【问题标题】:java.lang.IllegalStateException : Could not find a method onClick handler with id_buttonjava.lang.IllegalStateException:找不到带有 id_button 的方法 onClick 处理程序
【发布时间】:2014-01-12 16:02:34
【问题描述】:

我想问一下我的项目。我创建了一个类 (RestoranView.class),它有 3 个按钮:菜单、地图和评级。其中两个(地图和评级)运行良好,但是当我单击“菜单”按钮时,它不起作用,并且 logcat 显示了这些错误。我已经实现了相同的代码。你能帮我解决这个错误吗?提前致谢。

Logcat 错误

01-12 22:04:28.543: E/AndroidRuntime(25625): FATAL EXCEPTION: main
01-12 22:04:28.543: E/AndroidRuntime(25625): java.lang.IllegalStateException: Could not     find a method MenuClick(View) in the activity class com.example.hellojson.RestoranView for onClick handler on view class android.widget.Button with id 'button_menu'
01-12 22:04:28.543: E/AndroidRuntime(25625): at android.view.View$1.onClick(View.java:3685)
01-12 22:04:28.543: E/AndroidRuntime(25625): at android.view.View.performClick(View.java:4222)

RestoranView.java

ackage com.example.hellojson;

public class RestoranView extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.restoran_view);

// intent from RestoranView.class to the Map.class
public void MenuCLick (View v) {
    Intent menu = new Intent(RestoranView.this, TabMenu.class);
    menu.putExtra(Restoran.id_restoran_tags, resto.getId_restoran());
    startActivity(menu);
}

// intent from RestoranView.class to the Map.class
public void MapsClick(View v) {
    Intent maps = new Intent(RestoranView.this, Map.class);
    maps.putExtra(Restoran.nama_tags, resto.getNama());
    maps.putExtra(Restoran.latitude_tags, resto.getLatitude());
    maps.putExtra(Restoran.longitude_tags, resto.getLongitude());
    maps.putExtra(Restoran.desc_tags, resto.getDesc());
    startActivity(maps);

}

// intent from RestoranView.class to the Rating.class
public void RatingClick(View v) {
    Intent rating = new Intent(RestoranView.this, Rating.class);
    rating.putExtra(Restoran.id_restoran_tags, resto.getId_restoran());
    rating.putExtra(Restoran.nama_tags, resto.getNama());
    startActivity(rating);
    }
}

RestoranView.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="false" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/nama_restoran_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingBottom="10dip"
            android:paddingTop="5dip"
            android:text="Nama Restoran"
            android:textColor="@color/Navy"
            android:textSize="25sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/image_restoran_view"
            android:layout_width="275dp"
            android:layout_height="241dp"
            android:layout_gravity="center_horizontal"
            android:paddingTop="10dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/alamat_restoran_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
            android:text="Alamat :"
            android:textColor="@color/Navy"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/phone_restoran_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
            android:text="Phone : "
            android:textColor="@color/Navy"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/deskripsi_restoran_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
            android:text="Deskripsi"
            android:textColor="@color/Navy"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.30"
                android:onClick="MenuClick"
                android:text="Menu" />

            <Button
                android:id="@+id/button_maps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.30"
                android:onClick="MapsClick"
                android:text="Maps" />

            <Button
                android:id="@+id/button_rating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.30"
                android:onClick="RatingClick"
                android:text="Rating" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

</LinearLayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellojson"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.hellojson.SplashScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MenuUtama" >
    </activity>
    <activity android:name=".AboutMedan" >
    </activity>
    <activity android:name=".AllVenue" >
    </activity>
    <activity android:name=".TabMenu" >
    </activity>
    <activity android:name=".Map" >
    </activity>
    <activity android:name=".RestoranView" >
    </activity>
    <activity android:name=".Rating" >
    </activity>
    <activity android:name=".Search" >
    </activity>


</application>

</manifest>

【问题讨论】:

    标签: android xml logcat


    【解决方案1】:
    Could not find a method MenuClick(View) in the activity class com.example.hellojson.RestoranView for onClick handler on view class android.widget.Button with id 'button_menu'
    

    改变

     public void MenuCLick (View v)
    

     public void MenuClick (View v) { // l small
    

    因为你有

     android:onClick="MenuClick" // its MenuClick not MenuCLick
    

    正如托伯所说的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.restoran_view);
    } // missing }
    

    【讨论】:

    • 天哪,只有一个 L 字母会导致我好几天都无法修复的错误。谢谢指正……^_^
    • @user3187074 除了您错过了}onCreate 或者您在此处发帖时错过了。
    • @WulanRahmadiny,因为您是 stackoverflow 的新手,请查看此meta.stackexchange.com/questions/5234/…
    • 我已经修复了错误并且它可以工作。我也已经投票并检查了接受的答案。谢谢。
    【解决方案2】:

    您没有关闭 onCreate 方法...在 MenuClick 之前添加缺少的 }。

    【讨论】:

    • 谢谢@Tobor,我已经尝试过这个建议,错误是我用大“L”字母打错了“l”字母。
    【解决方案3】:

    在我的例子中,我没有在 onClick 函数中添加 View 参数

    改变了

    public void sendMessage()
    

    public void sendMessage(View v)
    

    【讨论】:

      【解决方案4】:

      在我的情况下,在 android lolly 上,从 xml 中删除主题属性并将主题放在 android manifest 中。它应该开始工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2017-09-30
        • 1970-01-01
        • 2017-12-08
        相关资源
        最近更新 更多