需要 onClick 处理程序才能查看
Posted
技术标签:
【中文标题】需要 onClick 处理程序才能查看【英文标题】:need onClick handler to view 【发布时间】:2016-02-16 11:00:14 【问题描述】:大家好.. 抱歉这个菜鸟问题。 我很难找到放置 onClick 的位置。 非常感谢您的帮助.. ^_^
这是日志猫
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_
android:layout_
android:orientation="vertical" >
<TextView
android:layout_
android:layout_
android:text="Kamus Inggris Indonesia" />
<TextView
android:text="Inggris :"
android:id="@+id/TextView01"
android:layout_
android:layout_></TextView>
<EditText
android:text=""
android:id="@+id/txtInggris"
android:layout_
android:layout_></EditText>
<Button
android:text="Terjemahkan"
android:id="@+id/btnTerjemah"
android:layout_
android:layout_
android:onClick="getTerjemahan"></Button>
<TextView
android:text="Indonesia :"
android:id="@+id/TextView03"
android:layout_
android:layout_></TextView>
<EditText
android:text=""
android:id="@+id/txtIndonesia"
android:layout_
android:layout_
android:editable="false"></EditText>
</LinearLayout>
【问题讨论】:
嗯,请您发布您的按钮的 xml 吗? 还有,把你的“setContentView(R.layout.main);”在 super.OnCreate 之后,他们把 datakamus = new .... @Coeus 添加了 xml 文件.. ^_^ 太棒了!看起来不错,你有没有按照我说的做? 你有错误的类,它说它在 indo.tora.kamus.KamusActivity 中期待它,而不是 indo.tora.kamus.ShowKamus . 【参考方案1】:看起来您的KamusActivity
使用main.xml
作为内容视图,但您没有将getTerjemahan
添加到此类。
所以你应该添加
public void getTerjemahan(View view)
//your code
到KamusActivity
。
【讨论】:
以上是关于需要 onClick 处理程序才能查看的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的edittext需要点两次才能触发,Onclick事件