我有一個xml
文件與ImageView
與android:onClick="playSound"
的propiety的可點擊的元素中,並能正常工作在第一加載列表視圖中我Activity
:當我點擊這個ImageView
它位於listview
的每一行,方法playSound
運行。安卓的onClick沒有找到方法的列表視圖
問題是我在這個Activity
的頂部有一個EditText
來過濾listview
中加載的內容,過濾元素/行,按名稱搜索(如字典)。當我寫的東西,和listview
有更少的元素,如果我點擊到任何行的同一ImageView
,出現強制關閉例外:NoSuchMethodException: playSound
看來onClick
找不到方法playSound
的Activity
內,但它在那兒。
的Activity
public class ListaCaracteres extends Activity{
private Context context;
private Cursor c;
private SQLiteDatabase hanyuDB;
private HanyuSQLHelper hanyuDBHelper;
private AdapterListaCaracteres adapter;
private ListView listaCaracteres;
private EditText buscador;
private ImageView play;
private MediaPlayer mediaPlayer;
private String texto = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(es.hsk.ap.R.layout.lista_caracteres);
/*
* Query a la base de datos, obtenemos todos los lugares en Cursor
*/
try{
hanyuDBHelper = new HanyuSQLHelper(this);
hanyuDB = hanyuDBHelper.getReadableDatabase();
c = hanyuDB.rawQuery(" SELECT * FROM Caracteres ", null);
}
catch(SQLiteException ex)
{
Toast mensajeError=Toast.makeText(getApplicationContext(), "Error accediendo a los datos", Toast.LENGTH_SHORT);
mensajeError.show();
Log.e("LogLugares", "Error en getReadableDatabase()", ex);
}
/*
* Utilizamos nuestro Adapter AdapterListaCaracteres con el contenido del Cursor
*/
adapter = new AdapterListaCaracteres(this,c, true);
listaCaracteres = (ListView)findViewById(es.hsk.ap.R.id.listaCaracteres);
listaCaracteres.setAdapter(adapter);
/*
* Localizamos y damos funciona al buscador
*/
buscador = (EditText)findViewById(R.id.buscador);
buscador.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
texto = buscador.getText().toString();
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
AdapterListaCaracteres adapter2 = new AdapterListaCaracteres(getApplicationContext(), c, false);
listaCaracteres.setAdapter(adapter2);
return false;
}
});
/*
* Ponemos acción al clic sobre un elemento del listview
*/
listaCaracteres.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
/*
* Al hacer clic se inicia activity MostrarCaracter
* El Intent enviado lleva como parámetro el id del objeto a mostrar
*/
if(texto!=null){
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
c.moveToPosition(position);
position=c.getInt(0)-1;
}
Intent intent=new Intent();
intent.setClass(getApplicationContext(), MostrarCaracter.class);
intent.putExtra("idObjeto", position);
startActivity(intent);
}
});
}
/*
* Método para reproducir sonido de carácter
*/
public void playSound(View view){
int posicion=listaCaracteres.getPositionForView(view);
if(texto!=null){
c = hanyuDB.rawQuery(
" SELECT * FROM Caracteres WHERE significado LIKE '%"
+ texto + "%'", null);
c.moveToPosition(posicion);
posicion=c.getInt(0)-1;
}
c = hanyuDB.rawQuery(" SELECT audio FROM Caracteres WHERE _id="+(posicion+1), null);
c.moveToFirst();
String audio=c.getString(0);
int resID = getResources().getIdentifier(audio, "raw", "es.hsk.ap");
mediaPlayer = MediaPlayer.create(this, resID);
mediaPlayer.start();
}
在我的適配器xml
膨脹的文件,誰擁有ImageView (id play)
是可以點擊的,與onClick
行動
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp" >
<TextView
android:id="@+id/caracter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
android:textSize="28dp" />
<TextView
android:id="@+id/pinyin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="right" >
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/icono"
android:paddingRight="7dp"
android:onClick="playSound"
android:src="@drawable/speaker" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp" >
<TextView
android:id="@+id/significado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="16dp" />
</TableRow>
的logcat:
04-04 21:36:07.144: E/AndroidRuntime(5569): Uncaught handler: thread main exiting due to uncaught exception
04-04 21:36:07.164: E/AndroidRuntime(5569): java.lang.IllegalStateException: Could not find a method reproducir(View) in the activity
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View$1.onClick(View.java:2020)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.performClick(View.java:2364)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.onTouchEvent(View.java:4179)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View.dispatchTouchEvent(View.java:3709)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.os.Looper.loop(Looper.java:123)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-04 21:36:07.164: E/AndroidRuntime(5569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-04 21:36:07.164: E/AndroidRuntime(5569): at dalvik.system.NativeStart.main(Native Method)
04-04 21:36:07.164: E/AndroidRuntime(5569): Caused by: java.lang.NoSuchMethodException: reproducir
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
04-04 21:36:07.164: E/AndroidRuntime(5569): at java.lang.Class.getMethod(Class.java:1014)
04-04 21:36:07.164: E/AndroidRuntime(5569): at android.view.View$1.onClick(View.java:2017)
我的自定義適配器:
public class AdapterListaCaracteres extends CursorAdapter{
private Context mContext;
private Cursor datos;
private boolean aRequery;
private LayoutInflater inflater;
/*
* Constructor de la clase
*/
public AdapterListaCaracteres(Context context, Cursor c, boolean autoRequery){
super(context, c, autoRequery);
this.mContext = context;
this.datos = c;
this.aRequery=autoRequery;
}
public void reproducir(View view){
}
/*
* Métodos
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return datos.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datos.getString(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View item = convertView;
ViewHolder holder;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
datos.moveToPosition(position);
//Comprobamos si el item existe para reaprovecharlo
if(item==null){
try{
item = inflater.inflate(es.hsk.ap.R.layout.caracter, null);
}
catch(InflateException ex)
{
// lo que querais hacer en este caso,mostrar un toast o lo que sea
}
holder = new ViewHolder();
holder.caracter = (TextView)item.findViewById(es.hsk.ap.R.id.caracter);
holder.pinyin = (TextView)item.findViewById(es.hsk.ap.R.id.pinyin);
holder.significado = (TextView)item.findViewById(es.hsk.ap.R.id.significado);
holder.audio = (ImageView)item.findViewById(es.hsk.ap.R.id.play);
item.setTag(holder);
}
else{
holder = (ViewHolder)item.getTag();
}
holder.caracter.setText(datos.getString(2));
holder.pinyin.setText(datos.getString(3));
holder.significado.setText(datos.getString(4));
return item;
}
/*
* Utilizamos clase ViewHolder para obtener IDs de objetos View inflados anteriormente
* (objetos hijos del objeto convertView) y así reparovecharlos para ahorrar recursos
* y batería al hacer scroll. Se localizan mediante la propiedad Tag
*/
static class ViewHolder{
TextView caracter;
TextView pinyin;
TextView significado;
ImageView audio;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
我錯了還是說LogCat中的方法「reproducir」?而在你的XML中,你有「playSound」聲明爲回調。 – Dan 2012-04-05 02:15:19
是的,但不用擔心,我已經改變它張貼在這裏,把一個英文方法的名字!抱歉! – Pelanes 2012-04-05 16:32:09