gpt4 book ai didi

java - 移动 TextView 框使我的 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 15:19:29 24 4
gpt4 key购买 nike

这个问题出现过几次,但我不明白为什么会发生。发生的事情是在我的 TextView 框周围移动一些东西最终出现在 EditText 框的顶部,这不好。所以我将 TextView 框移动到屏幕底部。当我这样做时,当我尝试访问存钱 jar 时,应用程序会崩溃。但是,如果我再次将 TextView 框移到顶部,它就可以正常工作..我真的不明白。无论如何,这是我得到的错误

06-22 09:06:41.928: E/AndroidRuntime(10958): java.lang.RuntimeException: Unable to
start activity ComponentInfo{net.finalexam/net.finalexam.Piggy}:
java.lang.ClassCastException: android.widget.RadioButton

这是小 pig 的 xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/piggy" >

<EditText
android:id="@+id/txtQuarters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:ems="10"
android:hint="Number of quarters"
android:inputType="number" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/txtDimes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtQuarters"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of dimes"
android:inputType="number" />

<EditText
android:id="@+id/txtNickles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtDimes"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of nickles"
android:inputType="number" />

<EditText
android:id="@+id/txtPennies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtNickles"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of pennies"
android:inputType="number" />


<EditText
android:id="@+id/txtDollars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPennies"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Number of Dollars"
android:inputType="number" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtDollars"
android:layout_centerHorizontal="true" >

<RadioButton
android:id="@+id/radAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Add" />

<RadioButton
android:id="@+id/radSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract" />
</RadioGroup>

<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup1"
android:layout_centerHorizontal="true"
android:text="Calculate" />

<TextView
android:id="@+id/txtResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCalculate"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:textSize="40sp" android:textStyle="bold" android:textColor="#000000"/>

</RelativeLayout>

这是 Piggy Java 文件

package net.finalexam;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Piggy extends Activity
{
double quartersValue = .25;
double dimesValue = .10;
double nicklesValue = .05;
double penniesValue = .01;
double dollarsValue = 1;
double quartersMoney;
double dollarsMoney;
double dimesMoney;
double nicklesMoney;
double penniesMoney;
double totalMoney;
double newTotalMoney;
double oldTotalMoney = 0;
int numberOfQuarters;
int numberOfDimes;
int numberOfNickles;
int numberOfPennies;
int numberOfDollars;



public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.piggybank);
final EditText quarters = (EditText) findViewById(R.id.txtQuarters);
final EditText dimes = (EditText) findViewById(R.id.txtDimes);
final EditText nickles = (EditText) findViewById(R.id.txtNickles);
final EditText pennies = (EditText) findViewById(R.id.txtPennies);
final EditText dollars = (EditText) findViewById(R.id.txtDollars);
Button calculate = (Button) findViewById(R.id.btnCalculate);
final TextView results = ((TextView) findViewById(R.id.txtResults));
final RadioButton add = (RadioButton) findViewById(R.id.radAdd);
final RadioButton subtract = (RadioButton) findViewById(R.id.radSubtract);

calculate.setOnClickListener(new OnClickListener()
{

public void onClick(View v)
{
if (quarters.getText().toString().equals(""))
{
numberOfQuarters = 0;
}
else
{
numberOfQuarters = Integer.parseInt(quarters.getText().toString());
}

if (dimes.getText().toString().equals(""))
{
numberOfDimes = 0;
}
else
{
numberOfDimes = Integer.parseInt(dimes.getText().toString());
}

if (nickles.getText().toString().equals(""))
{
numberOfNickles = 0;
}
else
{
numberOfNickles = Integer.parseInt(nickles.getText().toString());
}

if (pennies.getText().toString().equals(""))
{
numberOfPennies = 0;
}
else
{
numberOfPennies = Integer.parseInt(pennies.getText().toString());
}

if (dollars.getText().toString().equals(""))
{
numberOfDollars = 0;
}
else
{
numberOfDollars = Integer.parseInt(dollars.getText().toString());
}
quartersMoney = numberOfQuarters * quartersValue;
dimesMoney = numberOfDimes * dimesValue;
nicklesMoney = numberOfNickles * nicklesValue;
penniesMoney = numberOfPennies * penniesValue;
dollarsMoney = numberOfDollars;
totalMoney = quartersMoney + dimesMoney + nicklesMoney + penniesMoney + dollarsMoney;
DecimalFormat currency = new DecimalFormat("$###,###.##");

if (add.isChecked())
{
if (totalMoney > 0)
{
newTotalMoney = oldTotalMoney + totalMoney;
oldTotalMoney = newTotalMoney;
results.setText(currency.format(newTotalMoney));
}
else
{
Toast.makeText(Piggy.this, "You need to do more chores!!", Toast.LENGTH_LONG).show();

}
}
if (subtract.isChecked())
{
newTotalMoney = oldTotalMoney - totalMoney;
}
if (newTotalMoney > 0)
{

oldTotalMoney = newTotalMoney;
results.setText(currency.format(newTotalMoney));
}
else
{
Toast.makeText(Piggy.this, "Save more money kido!!", Toast.LENGTH_LONG).show();
};
}
});
}

}

我已经寻找答案,但情况似乎都略有不同。有任何想法吗?谢谢。正如我所说,如果 TextView 框与四分之一的 EditText 框重叠,则效果很好。

编辑1 这是存钱 jar 前的屏幕

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/ic_launcher_money"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="2px"
android:src="@drawable/ic_launcher_money"></ImageView>

<TextView
android:id="@+id/bankses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/bankses"
android:textSize="25sp">

</TextView>
</LinearLayout>

Main.java

package net.finalexam;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[] banks ={"Piggy Bank","Adult Bank"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.main, R.id.bankses, banks));
}
protected void onListItemClick(ListView l, View v, int position, long id){
switch(position){
case 0:
startActivity(new Intent(Main.this,Piggy.class));
break;
case 1:
startActivity(new Intent(Main.this,Adultbank.class));
break;
}
}

}

最佳答案

尝试在 Eclipse 上清理您的项目。它通常可以解决此类问题。

关于java - 移动 TextView 框使我的 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11156878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com