我正在单击按钮创建一个对话框。为此,我创建了一个扩展 Dialog 的类。我为每个文本视图创建了一个 XML 文件,其中包含该 XML 文件中的选项(都是文本视图 [7 文本视图])我定义了一个标签 android:onClick="imageProcessingHandler"。当我单击活动中的按钮时,它会打开一个很好的对话框。现在,当我单击任何文本视图时,我收到一个错误,即未定义函数。我在类中将此函数定义为扩展 Dialog 的 public。有人可以帮忙吗?
如果我想访问 Imageview,还有一件事;在扩展对话框的类中,从打开对话框的活动中,我该怎么做呢?我正在尝试执行以下操作: ImageView imgView = (ImageView) findViewById(R.id.fullphoto); 但在这种情况下 imgView 包含空值:(
代码:
从按钮上的活动“MyActivity”单击我正在创建一个对象:
ShowOptionsInDialog displayDialog = new ShowOptionsInDialog(this, passedData);
displayDialog.show();
ShowOptionsInDialog 类:
public class ShowOptionsInDialog extends Dialog{
private Context context;
public ShowOptionsInDialog(Context context, ArrayList<String> data) {
super(context);
this.context = (Context) context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageprocessingoptions);
setTitle("Try options");
}
public void imageProcessingHandler(View view){
ImageView photo = (ImageView) findViewById(R.id.fullphoto);
}
图像处理选项.XML
<TextView
android:id="@+id/invertColorText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:cursorVisible="false"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:gravity="bottom"
android:text="Invert Color"
android:textSize="15dp"
android:clickable="true"
android:onClick="imageProcessingHandler" />
活动代码:
public class PhotoHandler extends Activity {
private Bundle intentExtra;
private String photoID;
private CommomObject _commonObject;
private Facebook facebook;
private ImageView photo;
private DownloadImageTask downlaodImageTask;
private ProgressDialog progDialog;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.photohandler);
intentExtra = getIntent().getExtras();
photoID = intentExtra.getString("PHOTO_ID");
displayPhoto();
}
private void displayPhoto() {
_commonObject = CommomObject.getInstance();
facebook = _commonObject.getFacebookInstance();
String imageURL = "https://graph.facebook.com/"+photoID+"/picture?type=normal&access_token="+facebook.getAccessToken();
photo = (ImageView) findViewById(R.id.fullphoto);
photo.setTag(imageURL);
photo.setScaleType(ScaleType.FIT_CENTER);
downlaodImageTask = new DownloadImageTask();
progDialog = new ProgressDialog(this);
progDialog.setMessage("Fetching photo...");
progDialog.show();
downlaodImageTask.execute(photo);
}
public class DownloadImageTask extends AsyncTask<ImageView, Void, Bitmap>{
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews){
this.imageView = imageViews[0];
return downloadImage((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap downloadImage(String url) {
Bitmap photo = null;
try {
URL u = new URL(url);
URLConnection c = u.openConnection();
c.connect();
BufferedInputStream stream = new BufferedInputStream(c.getInputStream());
photo = BitmapFactory.decodeStream(stream);
stream.close();
} catch (MalformedURLException e) {
Log.e("PhotoHandler", "malformed url: " + url);
} catch (IOException e) {
Log.e("PhotoHandler", "An error has occurred downloading the image: " + url);
}
progDialog.dismiss();
return photo;
}
}
public void footerHandler(View view){
switch(view.getId()){
case R.id.useButton:
break;
case R.id.saveButton:
break;
case R.id.shareButton:
break;
case R.id.photoEffect:
ArrayList<String> passedData = new ArrayList<String>();
ShowOptionsInDialog displayDialog = new ShowOptionsInDialog(this, passedData);
displayDialog.show();
break;
}
}
public void imageProcessingHandler(View view){
switch(view.getId()){
case R.id.invertColorText:
Bitmap newBitmap = ImageProcessing.doInvert(photo.getDrawingCache());
photo.setImageBitmap(newBitmap);
break;
case R.id.greyScaleText:
break;
case R.id.gammaCorrectionText:
break;
case R.id.sepiaEffectText:
break;
case R.id.embossingEffectText:
break;
case R.id.reflectionEffectText:
break;
case R.id.contrastText:
break;
}
}
}