Java 应用程序崩溃,无错误
我使用的是Android Studio,我的应用程序在打开模拟器时就会崩溃,即使代码中没有错误 MainActivity.javaJava 应用程序崩溃,无错误,java,android,Java,Android,我使用的是Android Studio,我的应用程序在打开模拟器时就会崩溃,即使代码中没有错误 MainActivity.java package com.example.ss.flashcash; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.vie
package com.example.ss.flashcash;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends ActionBarActivity {
double money;
double convert;
EditText txtMoney;
RadioButton radEuros, radPesos, radRupees;
TextView txtResult;
Button btnComputeCost;
DecimalFormat hundredth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMoney = (EditText) findViewById(R.id.txtMoney);
radEuros = (RadioButton) findViewById(R.id.radEuros);
radPesos = (RadioButton) findViewById(R.id.radPesos);
radRupees = (RadioButton) findViewById(R.id.radRupees);
btnComputeCost = (Button) findViewById(R.id.btnComputeCost);
txtResult = (TextView) findViewById(R.id.txtResult);
btnComputeCost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate();
}
});
}
public void calculate()
{
hundredth = new DecimalFormat("#.##");
money = Double.parseDouble(txtMoney.getText().toString());
if (radEuros.isChecked())
{
if (money < 100000)
{
convert = money * .92;
txtResult.setText("$" + hundredth.format(convert));
}
else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
}
if (radPesos.isChecked())
{
if (money < 100000)
{
convert = money * 15.02;
txtResult.setText("$" + hundredth.format(convert));
}
else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
}
if (radRupees.isChecked())
{
if (money < 100000)
{
convert = money * 62.32;
txtResult.setText("$" + hundredth.format(convert));
}
else Toast.makeText(this, "Enter less than 100,000", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
非常感谢您的帮助。可能会得到
ClassCastException
,因为这里:
txtMoney = (EditText) findViewById(R.id.txtMoney);
txtMoney
是TextView
的id,但试图转换EditText
可能会得到ClassCastException
,因为:
txtMoney = (EditText) findViewById(R.id.txtMoney);
txtMoney
是TextView
的id,但试图转换EditText
可能会得到ClassCastException
,因为:
txtMoney = (EditText) findViewById(R.id.txtMoney);
txtMoney
是TextView
的id,但试图转换EditText
可能会得到ClassCastException
,因为:
txtMoney = (EditText) findViewById(R.id.txtMoney);
txtMoney
是TextView
的id,但试图转换EditText
查看布局文件。你写了:
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtMoney"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:hint="Enter $Amount"
android:typeface="sans"/>
...
正在引发ClassCastException
,因为您试图将TextView
强制转换为EditText
我希望你明白了。看看你的布局文件。你写了:
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtMoney"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:hint="Enter $Amount"
android:typeface="sans"/>
...
正在引发ClassCastException
,因为您试图将TextView
强制转换为EditText
我希望你明白了。看看你的布局文件。你写了:
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtMoney"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:hint="Enter $Amount"
android:typeface="sans"/>
...
正在引发ClassCastException
,因为您试图将TextView
强制转换为EditText
我希望你明白了。看看你的布局文件。你写了:
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtMoney"
android:layout_above="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:hint="Enter $Amount"
android:typeface="sans"/>
...
正在引发ClassCastException
,因为您试图将TextView
强制转换为EditText
我希望你明白了这一点。也从日志猫发布异常。也从日志猫发布异常。也从日志猫发布异常。也从日志猫发布异常。也从日志猫发布异常。