我正在尝试制作一个简单的费率计算器应用。这将从两个微调器中获取多个值,并将结果乘以基于所选单选按钮的值。
这是我的Java codde
package com.example.ratecalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
public class ClaculateAddRateActivity extends Activity implements
OnClickListener
{
Spinner spColumn, spInch;
RadioButton rbColor, rbBlackWhite;
Button btCalculate;
TextView tvAmount;
private final int BLACK_AND_WHITE_MULTIUPLIER = 10;
private final int COLOR_MULTIUPLIER = 20;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_claculate_add_rate);
findViewsById();
btCalculate.setOnClickListener(this);
String inches[] = new String[21];
for (int i = 0; i < 21; i++)
{
new String();
inches[i] = String.valueOf(i);
}
String columns[] = new String[8];
for (int i = 0; i < 8; i++)
{
new String();
columns[i] = String.valueOf(i);
}
spInch.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
inches));
spColumn.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
columns));
}
private void findViewsById()
{
spColumn = (Spinner) findViewById(R.id.spColumn);
spInch = (Spinner) findViewById(R.id.spInch);
rbColor = (RadioButton) findViewById(R.id.rbColor);
rbBlackWhite = (RadioButton) findViewById(R.id.rbBlackWhite);
tvAmount = (TextView) findViewById(R.id.tvAmount);
btCalculate = (Button) findViewById(R.id.btCalculate);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btCalculate:
int multiplier = rbColor.isChecked() ? COLOR_MULTIUPLIER
: BLACK_AND_WHITE_MULTIUPLIER;
int column = Integer.parseInt((String) spColumn
.getSelectedItem());
int inch = Integer.parseInt((String) spInch.getSelectedItem());
tvAmount.setText((multiplier * column * inch) + "");
break;
}
}
}
这是XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e7e7e7"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Prothom Alo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@android:color/white" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Page 1" />
<RadioGroup
android:id="@+id/rbg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/label1"
android:layout_marginLeft="15dp" >
<RadioButton
android:id="@+id/rbColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Color" />
<RadioButton
android:id="@+id/rbBlackWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B&W" />
</RadioGroup>
<TextView
android:id="@+id/labelColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/rbg"
android:layout_below="@id/rbg"
android:text="Column" />
<Spinner
android:id="@+id/spColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/labelColumn"
android:layout_toRightOf="@id/labelColumn"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/labelColumn"
android:layout_toRightOf="@id/spColumn"
android:text="1-8" />
<TextView
android:id="@+id/labelInch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/rbg"
android:layout_below="@id/spColumn"
android:text="Column" />
<Spinner
android:id="@+id/spInch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/labelInch"
android:layout_toRightOf="@id/labelInch"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/labelInch"
android:layout_toRightOf="@id/spInch"
android:text="21" />
<Button
android:id="@+id/btCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spInch"
android:layout_margin="5dp"
android:text="Calculate" />
<TextView
android:id="@+id/labelTaka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/btCalculate"
android:layout_below="@id/btCalculate"
android:text="Taka:" />
<TextView
android:id="@+id/tvAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/labelTaka"
android:layout_toRightOf="@id/labelTaka" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
它没有显示任何错误,但是当我尝试在模拟器上启动它时。该过程停止了
意外地。这背后的原因是什么?我该怎么解决这个问题呢。
这是我的日志猫
02-28 01:27:00.439: I/ActivityManager(74): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.ratecalculator/.ClaculateAddRateActivity } from pid 231
02-28 01:27:00.650: I/ActivityManager(74): Start proc com.example.ratecalculator for activity com.example.ratecalculator/.ClaculateAddRateActivity: pid=1268 uid=10035 gids={}
02-28 01:27:01.560: D/AndroidRuntime(1268): Shutting down VM
02-28 01:27:01.560: W/dalvikvm(1268): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-28 01:27:01.569: E/AndroidRuntime(1268): FATAL EXCEPTION: main
02-28 01:27:01.569: E/AndroidRuntime(1268): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ratecalculator/com.example.ratecalculator.ClaculateAddRateActivity}: java.lang.ArrayIndexOutOfBoundsException
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.os.Looper.loop(Looper.java:123)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 01:27:01.569: E/AndroidRuntime(1268): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 01:27:01.569: E/AndroidRuntime(1268): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 01:27:01.569: E/AndroidRuntime(1268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 01:27:01.569: E/AndroidRuntime(1268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 01:27:01.569: E/AndroidRuntime(1268): at dalvik.system.NativeStart.main(Native Method)
02-28 01:27:01.569: E/AndroidRuntime(1268): Caused by: java.lang.ArrayIndexOutOfBoundsException
02-28 01:27:01.569: E/AndroidRuntime(1268): at com.example.ratecalculator.ClaculateAddRateActivity.onCreate(ClaculateAddRateActivity.java:36)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 01:27:01.569: E/AndroidRuntime(1268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-28 01:27:01.569: E/AndroidRuntime(1268): ... 11 more
02-28 01:27:01.579: W/ActivityManager(74): Force finishing activity com.example.ratecalculator/.ClaculateAddRateActivity
02-28 01:27:02.100: W/ActivityManager(74): Activity pause timeout for HistoryRecord{409055f0 com.example.ratecalculator/.ClaculateAddRateActivity}
02-28 01:27:02.150: D/dalvikvm(74): GC_CONCURRENT freed 688K, 55% free 4552K/9991K, external 1492K/1828K, paused 8ms+20ms
02-28 01:27:03.580: I/Process(1268): Sending signal. PID: 1268 SIG: 9
02-28 01:27:03.620: W/InputManagerService(74): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@406eed40
02-28 01:27:03.700: I/ActivityManager(74): Process com.example.ratecalculator (pid 1268) has died.
02-28 01:27:12.658: W/ActivityManager(74): Activity destroy timeout for HistoryRecord{409055f0 com.example.ratecalculator/.ClaculateAddRateActivity}
答案 0 :(得分:0)
spInch.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
inches));
spColumn.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
columns));
此ArrayAdapter
constructor接收layout
资源,android.R.id.text1
不是layout
资源。传入R.layout.your_layout
,其中your_layout
指的是res/layout
中包含TextView
的XML布局文件。
new String();
循环中的for
次呼叫无用,但不会导致崩溃。
您更新的问题中的ArrayIndexOutOfBoundsException
似乎是指第一个for
循环。似乎没有数组索引问题 - 来自以前版本的代码的logcat?
答案 1 :(得分:0)
在你的构造函数中,你没有在这里设置你的布局。
spInch.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
inches));
spColumn.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
columns));
其中 yourLayut 是您的 res 命名文件夹中提供的布局xml文件 res / layout 强>
所以它应该是这样的。
spInch.setAdapter(new ArrayAdapter<String>(this, R.layout.yourLayut , android.R.id.text1,
inches));
spColumn.setAdapter(new ArrayAdapter<String>(this, R.layout.yourLayut , android.R.id.text1,
columns));
它应该是这样的方式,它是它的语法。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, resource, textViewResourceId, objects)
修改强>
你应该改变这个,因为你已经将你的字符串数组定义为21和8,而对于for循环,你已经用0到8进行了检查,所以它的计数为第9项,它将大于你的String数组。所以你需要改变。
String inches[] = new String[21];
for (int i = 0; i < 20; i++)
{
new String();
inches[i] = String.valueOf(i);
}
String columns[] = new String[8];
for (int i = 0; i < 7; i++)
{
new String();
columns[i] = String.valueOf(i);
}