需要onClick处理程序来查看

时间:2015-11-15 05:47:39

标签: javascript android eclipse onclick logcat

hy伙伴们......对于noob问题感到抱歉。 我无法找到放置onClick的位置。 真的很感激帮助.. ^ _ ^

这是logcat

11-15 05:14:18.041: E/AndroidRuntime(433): FATAL EXCEPTION: main
11-15 05:14:18.041: E/AndroidRuntime(433): java.lang.IllegalStateException: Could not find a method getTerjemahan(View) in the activity class indo.tora.kamus.KamusActivity for onClick handler on view class android.widget.Button with id 'btnTerjemah'
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.view.View$1.onClick(View.java:2059)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.view.View.performClick(View.java:2408)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.view.View$PerformClick.run(View.java:8816)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.os.Handler.handleCallback(Handler.java:587)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.os.Looper.loop(Looper.java:123)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 05:14:18.041: E/AndroidRuntime(433):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 05:14:18.041: E/AndroidRuntime(433):  at java.lang.reflect.Method.invoke(Method.java:521)
11-15 05:14:18.041: E/AndroidRuntime(433):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 05:14:18.041: E/AndroidRuntime(433):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 05:14:18.041: E/AndroidRuntime(433):  at dalvik.system.NativeStart.main(Native Method)
11-15 05:14:18.041: E/AndroidRuntime(433): Caused by: java.lang.NoSuchMethodException: getTerjemahan
11-15 05:14:18.041: E/AndroidRuntime(433):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
11-15 05:14:18.041: E/AndroidRuntime(433):  at java.lang.Class.getMethod(Class.java:985)
11-15 05:14:18.041: E/AndroidRuntime(433):  at android.view.View$1.onClick(View.java:2052)

这是需要点击广告的类.. ShowKamus.java

package indo.tora.kamus;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;



public class ShowKamus extends Activity {


    private SQLiteDatabase db = null;
    private Cursor kamusCursor = null;
    private EditText txtInggris;
    private EditText txtIndonesia;
    private DataKamus datakamus = null;
    Button translate;
    public static final String INGGRIS = "inggris";
    public static final String INDONESIA = "indonesia";



     /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        datakamus = new DataKamus(this);

        db = datakamus.getWritableDatabase();

        datakamus.createTable(db);

        datakamus.generateData(db);



        setContentView(R.layout.main);

        txtInggris = (EditText) findViewById(R.id.txtInggris);
        txtIndonesia = (EditText) findViewById(R.id.txtIndonesia);



    }



    public void getTerjemahan(View view) {

        String result = "";

        String englishword = txtInggris.getText().toString();

        kamusCursor = db.rawQuery("SELECT ID, INGGRIS, INDONESIA " + "FROM kamus where INGGRIS='" + englishword + "' ORDER BY INGGRIS", null);



        if (kamusCursor.moveToFirst()) {

            result = kamusCursor.getString(2);

            for (; !kamusCursor.isAfterLast(); kamusCursor.moveToNext()) {

                result = kamusCursor.getString(2);

                }

        }

        if (result.equals("")) {

            result = "Terjemahan Not Found";

        }

        txtIndonesia.setText(result);



    }

    @Override

    public void onDestroy() {

        super.onDestroy();

        kamusCursor.close();

        db.close();

    }

}

提前感谢..

xml文件.. main.xml

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

     <TextView 
         android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Kamus Inggris Indonesia" />

     <TextView 
        android:text="Inggris :" 
        android:id="@+id/TextView01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></TextView>

    <EditText 
        android:text="" 
        android:id="@+id/txtInggris"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"></EditText>

    <Button 
        android:text="Terjemahkan" 
        android:id="@+id/btnTerjemah"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getTerjemahan"></Button>

    <TextView 
        android:text="Indonesia :" 
        android:id="@+id/TextView03"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></TextView>

    <EditText 
        android:text="" 
        android:id="@+id/txtIndonesia"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:editable="false"></EditText>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

看起来您的KamusActivity使用main.xml作为内容视图,但您没有将getTerjemahan添加到此类。

所以你应该添加

 public void getTerjemahan(View view) {
    //your code
 }

KamusActivity