我刚刚开始使用android但已经做了一些c#,它看起来与java非常相似
简而言之,问题在于closeDialog方法
我对view / viewgroup不是很熟悉所以请不要用不正确的对象使用等来轰炸我。
简而言之,我正在创建一个简单的应用程序,我希望改进它(它基本上是一个巨大项目的开始)
_showhint对话框打开正常,并按预期显示“提示”,但closeDialog强制关闭应用程序,我不明白为什么
package com.example.app;
import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private WebView webView;
final Activity activity = this;
public Uri imageUri;
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageImageURI = null;
private TextView lblAnswer, lblWelcome;
private EditText edtInput;
public TextView showText ;
public Button btnShowHint, btnCalculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtInput = (EditText) findViewById(R.id.edtInput) ;
lblWelcome = (TextView) findViewById(R.id.lblWelcome) ;
lblAnswer = (TextView) findViewById(R.id.lblAnswer) ;
btnShowHint = (Button) findViewById(R.id.btnHelp);
btnCalculate = (Button) findViewById(R.id.btnShow) ;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void calculate(View vw)
{
String [] arrEditStore = new String[edtInput.length()] ;
String arrOperators [] = {"+", "-", "*", "/", "(", ")"} ;
}
public void _showhint(View vw)
{
final Dialog showHintDialog = new Dialog(activity);
showHintDialog.setContentView(R.layout.custom_dialog);
showHintDialog.setTitle("How to enter data");
showHintDialog.show();
}
public void closeDialog(View vw)
{
final Dialog dialog = new Dialog(this) ;
Button btnClose = (Button) dialog.findViewById(R.id.button) ;
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
编辑:将“this”添加到final Dialog dialog = new Dialog(this)
答案 0 :(得分:0)
我发现了导致问题的原因
GIVEN CODE:
public void _showhint(View vw) { final Dialog showHintDialog = new Dialog(activity); showHintDialog.setContentView(R.layout.custom_dialog); showHintDialog.setTitle("How to enter data"); showHintDialog.show(); } public void closeDialog(View vw) { final Dialog dialog = new Dialog(this) ; Button btnClose = (Button) dialog.findViewById(R.id.button) ; btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); }
<强> SOULTION 强>
首先,我将btnClose的decleration移到了顶部
public Button btnShowHint, btnCalculate, btnClose;
然后在设计视图中,删除按钮的链接关闭onclick方法,参考closeDialog。
然后,完全删除closeDialog方法,并将部分代码移动到_showHint方法
它也具有逻辑意义,感谢@Mike M.对帖子发表评论,帮我推理出来,因为我说,这个按钮必须关闭对话框但是在这个按钮的方法中,我将它分配给单独使用,它根本没有逻辑意义,这里是改变的代码,它的工作原理
更改代码:
public void _showhint(View vw)
{
final Dialog showHintDialog = new Dialog(activity);
showHintDialog.setContentView(R.layout.custom_dialog);
showHintDialog.setTitle("How to enter data");
showHintDialog.show();
btnClose = (Button) showHintDialog.findViewById(R.id.button) ;
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showHintDialog.dismiss();
}
});
}
我们有时会犯愚蠢和不合时宜的错误,有些比其他人更糟糕,但是如果你找到了解决问题的方法,也许某个人有同样的问题,那么就可以解决你的问题,这可能有所帮助那个人!!!
欢呼声