2012-09-10 142 views
0

我遇到問題,我的應用程序意外停止。這是一個簡單的應用程序,讓用戶輸入骰子數和過濾器編號。它意外崩潰,即使我多次清理它。 這裏是java文件:Android應用程序意外崩潰

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> 
+0

您的應用程序正在試圖尋找一個可繪製名爲'#000000'。這是黑色的顏色,不是可繪製的。我覺得你已經把它放在你的XML的某個地方,它不屬於它。 – Eric

+0

logcat告訴你這是你的佈局XML文件中的錯誤,在#2行。請張貼該文件,以便我們提供幫助。 – Huang

+0

是的。這是我的XML的背景顏色。 –

回答

0

我懷疑你的問題是在XML文件中,在這裏:

android:background="@string/backgroundcolor" 

你在做什麼這裏傳遞一個字符串,應該採取一種顏色或繪製參考的字段。據我所知,如果有問題的字符串是有效的顏色字符串(如#012),則無關緊要。這不會像預處理宏那樣被替換爲文本,而是會在運行時查找它,並獲得它不期望的值。

爲了解決這個問題,從

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

的定義修改爲

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

,並指它在佈局文件

android:background="@color/backgroundcolor" 
+0

非常感謝解決方案。但是,我使用#000000而不是字符串,它的工作。 –

+0

哎呀我實際上混淆了我所說的,意思是說從字符串到顏色的變化,而不是相反(現在將編輯):P很高興我能幫忙,雖然:) – Xono

+0

謝謝。希望別人能看到它並修正它們的程序:D –

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); 
} 

};