1

我刚刚开始在 Android 中编码,下图是具有基本操作的简单计算器,当我在 SONY ERICSSON LIVE WITH WALKMAN 上运行它时,我收到消息为“不幸的是 CACULATOR 已停止”下图是控制台输出:

[2013-01-22 11:42:52 - calci] ------------------------------
[2013-01-22 11:42:52 - calci] Android Launch!
[2013-01-22 11:42:52 - calci] adb is running normally.
[2013-01-22 11:42:52 - calci] Performing com.example.calci.MainActivity activity launch
[2013-01-22 11:42:54 - calci] Uploading calci.apk onto device 'BX902W2BWH'
[2013-01-22 11:42:54 - calci] Installing calci.apk...
[2013-01-22 11:42:57 - calci] Success!
[2013-01-22 11:42:57 - calci] Starting activity com.example.calci.MainActivity on device BX902W2BWH
[2013-01-22 11:42:58 - calci] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.calci/.MainActivity }

这是 LogCat 窗口消息:

01-22 11:42:32.071: D/AndroidRuntime(4046): Shutting down VM
01-22 11:42:32.071: W/dalvikvm(4046): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-22 11:42:32.081: E/AndroidRuntime(4046): FATAL EXCEPTION: main
01-22 11:42:32.081: E/AndroidRuntime(4046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calci/com.example.calci.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.os.Looper.loop(Looper.java:137)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at dalvik.system.NativeStart.main(Native Method)
01-22 11:42:32.081: E/AndroidRuntime(4046): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.Activity.setContentView(Activity.java:1835)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at com.example.calci.MainActivity.onCreate(MainActivity.java:21)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.Activity.performCreate(Activity.java:4465)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-22 11:42:32.081: E/AndroidRuntime(4046):     ... 11 more
01-22 11:42:32.081: E/AndroidRuntime(4046): Caused by: java.lang.reflect.InvocationTargetException
01-22 11:42:32.081: E/AndroidRuntime(4046):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
01-22 11:42:32.081: E/AndroidRuntime(4046):     ... 23 more
01-22 11:42:32.081: E/AndroidRuntime(4046): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.content.res.Resources.loadDrawable(Resources.java:1897)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.View.<init>(View.java:2804)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.View.<init>(View.java:2741)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.view.ViewGroup.<init>(ViewGroup.java:412)
01-22 11:42:32.081: E/AndroidRuntime(4046):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
01-22 11:42:32.081: E/AndroidRuntime(4046):     ... 26 more
01-22 11:43:03.361: D/AndroidRuntime(4197): Shutting down VM
01-22 11:43:03.361: W/dalvikvm(4197): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-22 11:43:03.371: E/AndroidRuntime(4197): FATAL EXCEPTION: main
01-22 11:43:03.371: E/AndroidRuntime(4197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calci/com.example.calci.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.os.Looper.loop(Looper.java:137)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at dalvik.system.NativeStart.main(Native Method)
01-22 11:43:03.371: E/AndroidRuntime(4197): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.Activity.setContentView(Activity.java:1835)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at com.example.calci.MainActivity.onCreate(MainActivity.java:21)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.Activity.performCreate(Activity.java:4465)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-22 11:43:03.371: E/AndroidRuntime(4197):     ... 11 more
01-22 11:43:03.371: E/AndroidRuntime(4197): Caused by: java.lang.reflect.InvocationTargetException
01-22 11:43:03.371: E/AndroidRuntime(4197):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
01-22 11:43:03.371: E/AndroidRuntime(4197):     ... 23 more
01-22 11:43:03.371: E/AndroidRuntime(4197): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.content.res.Resources.loadDrawable(Resources.java:1897)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.View.<init>(View.java:2804)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.View.<init>(View.java:2741)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.view.ViewGroup.<init>(ViewGroup.java:412)
01-22 11:43:03.371: E/AndroidRuntime(4197):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
01-22 11:43:03.371: E/AndroidRuntime(4197):     ... 26 more

对不起,如果我无法识别简单的错误..!

这是我的 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@style/AppTheme"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:hint="@string/enter_first_number"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="26dp"
        android:ems="10"
        android:hint="Enter Second Number"
        android:inputType="number" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:text="SUB" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="45dp"
        android:text="MUL" />

    <Button 
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignLeft="@+id/button2"
        android:text="DIV" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="66dp"
        android:text="ADD" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="24dp"
        android:hint="RESULT"
        android:text="RESULT" />

</RelativeLayout>
4

3 回答 3

1

这是什么: android:background="@style/AppTheme" ?这就是问题。

您不能将主题设置为背景。它应该是彩色的或可绘制的。

于 2013-01-22T06:55:28.640 回答
0

我以前没有在 Android 中编程,但我有 JAVA 经验。对我来说,您似乎正在使用无法绘制到设备 UI 上的资源。

这似乎是问题所在:

01-22 11:42:32.081: E/AndroidRuntime(4046): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}

再次检查 XML 是否有错误的资源或语法错误。如果您仍然收到错误,请在此处发布您的 XML 文件。

于 2013-01-22T06:24:34.967 回答
0

不确定,但我注意到在 EditText1 中你有android:hint="@string/enter_first_number",但在 editText2 中android:hint="Enter Second Number"- 应该是这样android:hint="@String/Enter Second Number"吗?

于 2013-01-22T06:53:38.437 回答