有什么简单的东西可能会丢失吗? “kruis”图片显示在我的ImageButton上,所以我非常确定我的main.xml是好的,但是当我点击ImageButton时,我没有Toast,而testView也没有改变......一直在挣扎几个小时现在,不知道我做错了什么!
package com.matchit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class matchit extends Activity {
OnClickListener cardListener;
TextView testView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testView = (TextView)findViewById(R.id.test);
ImageButton b1 = (ImageButton)findViewById(R.id.card1);
b1.setImageResource(R.drawable.kruis);
b1.setOnClickListener(cardListener);
cardListener = new OnClickListener(){
@Override
public void onClick(View v) {
testView.setText("its working");
Toast.makeText(getApplicationContext(),
"its working",
Toast.LENGTH_LONG).show();
}
};
}
}
答案 0 :(得分:5)
cardListener
的值在此行为空:
b1.setOnClickListener(cardListener);
在cardListener = new OnClickListener(){
cardListener = new OnClickListener(){
@Override
public void onClick(View v) {
testView.setText("its working");
Toast.makeText(getApplicationContext(),
"its working",
Toast.LENGTH_LONG).show();
}
};
// moved down
b1.setOnClickListener(cardListener);