Android应用程序意外崩溃

时间:2012-09-10 00:00:59

标签: android

我遇到了一个问题,我的应用程序意外停止了。这是一个简单的应用程序,让用户输入骰子数和过滤器数。即使我多次清理它,它也会意外崩溃。 这是java文件:

package com.greg.rostov.dice;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class DiceRoller extends Activity {
DiceRoller r=new DiceRoller();

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button roll=(Button)findViewById(R.id.roll);

    roll.setOnClickListener(onRoll);
}
private View.OnClickListener onRoll=new View.OnClickListener() {
        public void onClick(View v){
            EditText numberofdice=(EditText)findViewById(R.id.roll);
            EditText filterdice=(EditText)findViewById(R.id.roll);
        }
    };




private String numberofdice="";
private String filterdice="";

public String getNumberofdice() {
    return(numberofdice);
}
public void setNumberofdice(String numberofdice){
    this.numberofdice=numberofdice;
}

public String getFilterdice() {
    return(filterdice);
}
public void setFilterdice(String filterdice){
    this.filterdice=filterdice;
}
}

这是错误日志:

    09-10 09:41:09.937: D/AndroidRuntime(551): Shutting down VM
    09-10 09:41:09.937: W/dalvikvm(551): threadid=1: thread exiting with uncaught exception(group=0x40015560)
    09-10 09:41:09.977: E/AndroidRuntime(551): FATAL EXCEPTION: main
    09-10 09:41:09.977: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.greg.rostov.dice/com.greg.rostov.dice.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.TableLayout
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.os.Looper.loop(Looper.java:123)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-10 09:41:09.977: E/AndroidRuntime(551):  at java.lang.reflect.Method.invokeNative(Native Method)
09-10 09:41:09.977: E/AndroidRuntime(551):  at java.lang.reflect.Method.invoke(Method.java:507)
09-10 09:41:09.977: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 09:41:09.977: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 09:41:09.977: E/AndroidRuntime(551):  at dalvik.system.NativeStart.main(Native Method)
09-10 09:41:09.977: E/AndroidRuntime(551): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.TableLayout
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-10 09:41:09.977: E/AndroidRuntime(551):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-10 09:41:09.977: E/AndroidRuntime(551):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.Activity.setContentView(Activity.java:1657)
09-10 09:41:09.977: E/AndroidRuntime(551):  at com.greg.rostov.dice.MainActivity.onCreate(MainActivity.java:14)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-10 09:41:09.977: E/AndroidRuntime(551):  ... 11 more
09-10 09:41:09.977: E/AndroidRuntime(551): Caused by: java.lang.reflect.InvocationTargetException
09-10 09:41:09.977: E/AndroidRuntime(551):  at java.lang.reflect.Constructor.constructNative(Native Method)
09-10 09:41:09.977: E/AndroidRuntime(551):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-10 09:41:09.977: E/AndroidRuntime(551):  ... 21 more
09-10 09:41:09.977: E/AndroidRuntime(551): Caused by: android.content.res.Resources$NotFoundException: File #000000 from drawable resource ID #0x7f050004
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.content.res.Resources.loadDrawable(Resources.java:1714)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.View.<init>(View.java:1951)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.View.<init>(View.java:1899)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.view.ViewGroup.<init>(ViewGroup.java:286)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.widget.LinearLayout.<init>(LinearLayout.java:120)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.widget.TableLayout.<init>(TableLayout.java:105)
09-10 09:41:09.977: E/AndroidRuntime(551):  ... 24 more
09-10 09:41:09.977: E/AndroidRuntime(551): Caused by: java.io.FileNotFoundException: #000000
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.content.res.AssetManager.openNonAssetNative(Native Method)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.content.res.AssetManager.openNonAsset(AssetManager.java:406)
09-10 09:41:09.977: E/AndroidRuntime(551):  at android.content.res.Resources.loadDrawable(Resources.java:1706)
09-10 09:41:09.977: E/AndroidRuntime(551):  ... 30 more
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@string/backgroundcolor"
android:stretchColumns="1" 
>
<TableRow>
    <TextView 
     android:text="Dice:"
     android:textColor="@string/textcolor"
     android:textSize="25dp"/>

    <EditText
        android:id="@+id/dicebox"
        android:inputType="text" />
</TableRow>
<TableRow>
    <TextView 
    android:text="Filter"
    android:textColor="@string/textcolor"
    android:textSize="16dp"/>
    <EditText
    android:id="@+id/filterdicebox"/>
</TableRow>
<Button android:id="@+id/roll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/textonbutton"
    android:textSize="25dp"
    />
</TableLayout>

2 个答案:

答案 0 :(得分:0)

看看你的听众:

private View.OnClickListener onRoll=new View.OnClickListener() {
    public void onClick(View v){
        EditText numberofdice=(EditText)findViewById(R.id.roll);
        EditText filterdice=(EditText)findViewById(R.id.roll);
    }
};

您为EditText引用了相同的ID:numberofdice和filterdice。

将其更改为:

private View.OnClickListener onRoll=new View.OnClickListener() {
public void onClick(View v){
    EditText numberofdice=(EditText)findViewById(R.id.dicebox);
    EditText filterdice=(EditText)findViewById(R.id.filterdicebox);
}

};

答案 1 :(得分:0)

我怀疑你的问题出在你的XML文件中,在这里:

android:background="@string/backgroundcolor"

你在这里做的是将一个字符串传递给一个应该采用颜色或可绘制引用的字段。据我所知,如果所讨论的字符串是有效的颜色字符串(如#012)并不重要。这不会被视为替换文本的预处理器宏,而是在运行时查找它并获得它不期望的值。

要解决此问题,请从

更改定义
<string name="backgroundcolor">#000000</string>

<color name="backgroundcolor">#000000</color>

并在布局文件中将其称为

android:background="@color/backgroundcolor"