【问题标题】:My android application stopped unexpectedly我的 android 应用程序意外停止
【发布时间】:2014-03-03 16:34:47
【问题描述】:

我正在尝试制作一个简单的费率计算器应用程序。它将从两个微调器中获取值乘以它们,结果将乘以基于所选单选按钮的值。

这是我的 Java 代码

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&amp;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>

它没有显示任何错误,但是当我尝试在 emulator 上启动它时。进程停止

没想到。这背后的原因是什么?我该如何解决这个问题。

这是我的日志猫

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}

【问题讨论】:

  • 可以提供logcat吗?
  • 请放上你的logcat..
  • 没有 logcat 没什么好做的
  • 你得到这个错误了吗 03-03 13:46:36.806: E/AndroidRuntime(1317): android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014跨度>
  • 我提供了 log cat 。不,我没有得到这样的错误@rajshree

标签: android


【解决方案1】:
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 中的XML 布局文件,其中包含TextView

for 循环中的 new String(); 调用没有用,但不会导致崩溃。

您更新后的问题中的ArrayIndexOutOfBoundsException 似乎指的是第一个for 循环。那里似乎没有数组索引问题 - 您的代码的先前版本中的 logcat 吗?

【讨论】:

    【解决方案2】:

    在您的构造函数中,您尚未在此处设置布局。

     spInch.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
                inches));
     spColumn.setAdapter(new ArrayAdapter<String>(this, android.R.id.text1,
                columns));
    

    yourLayut 是您的布局 xml 文件,该文件位于您的 res 命名文件夹中,该文件夹分配有 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 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);
        }
    

    【讨论】:

    • 注意到 for 循环条件中的 &lt; 而不是 &lt;=?这似乎不是问题。这是 OP 还没有展示的东西。
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多