Android运行时错误
我是一名Android初学者,我的代码中有一个运行时错误:当我想在模拟器或设备中运行它时,它会显示“强制关闭”信息。我的日志在这里: 我的Java代码:Android运行时错误,android,runtime-error,Android,Runtime Error,我是一名Android初学者,我的代码中有一个运行时错误:当我想在模拟器或设备中运行它时,它会显示“强制关闭”信息。我的日志在这里: 我的Java代码: public class Third extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button d = (Button) findView
public class Third extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button d = (Button) findViewById(R.id.btn4);
d.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog();
}
});
}
public void dialog(){
final Dialog dialog = new Dialog(Third.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("واحد عدد وارد شده را انتخاب کنید");
dialog.show();
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.rg);
final RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.rb1);
final RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.rb2);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if(rb1.isChecked()) {
dialog.dismiss();
Button t = (Button)findViewById(R.id.btn5);
t.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText ft1 = (EditText)findViewById(R.id.f3);
TextView foot = (TextView)findViewById(R.id.foot);
TextView mile = (TextView)findViewById(R.id.mile);
TextView inch = (TextView)findViewById(R.id.inch);
TextView yard = (TextView)findViewById(R.id.yard);
TextView mm = (TextView)findViewById(R.id.millymeter);
TextView dm = (TextView)findViewById(R.id.decimeter);
TextView mim = (TextView)findViewById(R.id.micrometer);
TextView nm = (TextView)findViewById(R.id.nanometer);
TextView hand = (TextView)findViewById(R.id.hand);
TextView iron = (TextView)findViewById(R.id.iron);
TextView point = (TextView)findViewById(R.id.point);
if(ft1.getText().toString().length() == 0 ){return;}
int first = Integer.parseInt(ft1.getText().toString());
double equal = first *0.0328;
DecimalFormat formatf = new DecimalFormat("#.####");
String x = formatf.format(equal)+" فوت";
foot.setText(x);
first = Integer.parseInt(ft1.getText().toString());
equal = first * 0.000005;
DecimalFormat formatm = new DecimalFormat("#.####");
x = formatm .format(equal)+"مایل";
mile.setText(x);
equal = first * 0.393;
DecimalFormat formati = new DecimalFormat("#.####");
x = formati.format(equal)+"اینچ";
inch.setText(x);
equal = first * 0.0109;
DecimalFormat formaty = new DecimalFormat("#.#####");
x = formaty.format(equal)+"یارد";
yard.setText(x);
equal = first / 10;
DecimalFormat formatmi = new DecimalFormat("#.##");
x = formatmi.format(equal)+"دسی متر";
dm.setText(x);
int equalmm = first * 10;
x = equalmm+"میلی متر";
mm.setText(x);
int equalm = first * 10000;
x = equalm+"میکرو متر";
mim.setText(x);
int equaln = first * 10000000;
x = equaln + "نانو متر";
nm.setText(x);
equal = first * 0.098;
DecimalFormat formath = new DecimalFormat("#####.#####");
x = formath.format(equal)+"هَند";
hand.setText(x);
equal = first * 19;
x = equal+"آیرون";
iron.setText(x);
equal = first * 28;
x = equal+"پوینت";
point.setText(x);
}
});
}
在使用
findViewById()
方法检索视图之前,需要先使用布局资源对活动进行充气。如果没有布局,那么就没有可能找到的视图。您缺少一个重要部分。您需要在活动中添加一个视图
,以便找到该视图的元素,如您试图实例化的按钮
在OnCreate
方法中添加以下内容:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_xml_file); //Here add the xml file with the view you want to show
Button d = (Button) findViewById(R.id.btn4);
d.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog();
}
});
}
在super.onCreate(savedInstanceState)之后;您正在设置内容视图吗
e、 g
setContentView(R.layout.nameofxmlfilehere) 我有很多小部件,比如按钮或编辑文本,但它不工作,然后当我删除按钮代码时,它会进入活动,但不会显示任何内容谢谢你,我解决了它,正如你知道的Eclips,它不接受五月文本视图的ID,没有任何错误或红线我做了你的建议,但它不工作,我的活动中有一些东西。你说“我的活动中的一些东西”是什么意思?我的意思是我有一个像编辑文本或文本视图或按钮这样的视图。你可以发布主xml文件吗?对不起,我不知道为什么stack不能发布我的文件,我的xml文件在notpad中的.RAR文件中。谢谢。请注意,如果我删除按钮代码,我的应用程序将正确运行,但除任何视图外,您是否也已将活动添加到您的AndroidManifest文件中?谢谢,我解决了它,正如您知道的Eclips,它不接受may text view的ID,没有任何错误或红线