2

我一直在使用 android 应用程序时遇到困难。我正在使用 Eclipse,V 4.2.0,并运行 API 版本 7。下面是我遇到困难的一个程序的 LogCat 输出。这是我的代码:

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deadEddie.counter"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

</manifest>

布局(activity_main.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="#000000">

<TextView

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textColor="@string/color"
    android:textSize="30dp"
    android:text="@string/text"
    android:textStyle="bold"
    android:gravity="center"
    android:id="@+id/tvDisplay"

    />

 <Button
    android:id="@+id/btnAdd"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@string/btn1Title"
    android:layout_gravity="center" 
    />

  <Button
    android:id="@+id/btnSubtract"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@string/btn2Title"
    android:layout_gravity="center"
    />


</LinearLayout>

MainActivity.java

package com.deadEddie.counter;

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

public class MainActivity extends Activity 
{
int counter;
Button add;
Button subtract;
TextView display;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.btnAdd);
    subtract = (Button) findViewById(R.id.btnSubtract);
    display = (TextView) findViewById(R.id.tvDisplay);

    add.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            counter ++;
            display.setText("Counter = " + counter);

        }
    });
    subtract.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            counter --;
            display.setText("Counter = "+ counter);
        }
    });

}
}

LogCat 输出:

08-06 13:23:37.538: E/vold(26): Error opening switch name path '/sys/class/switch/test' (No such file or directory)
08-06 13:23:37.538: E/vold(26): Error bootstrapping switch '/sys/class/switch/test' (No such file or directory)
08-06 13:23:37.538: E/vold(26): Error opening switch name path '/sys/class/switch/test2' (No such file or directory)
08-06 13:23:37.538: E/vold(26): Error bootstrapping switch '/sys/class/switch/test2' (No such file or directory)
08-06 13:23:46.518: E/BatteryService(51): usbOnlinePath not found
08-06 13:23:46.518: E/BatteryService(51): batteryVoltagePath not found
08-06 13:23:46.518: E/BatteryService(51): batteryTemperaturePath not found
08-06 13:23:46.528: E/SurfaceFlinger(51): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
08-06 13:23:49.848: E/EventHub(51): could not get driver version for /dev/input/mouse0, Not a typewriter
08-06 13:23:49.848: E/EventHub(51): could not get driver version for /dev/input/mice, Not a typewriter
08-06 13:23:50.638: E/System(51): Failure starting core service
08-06 13:23:50.638: E/System(51): java.lang.SecurityException
08-06 13:23:50.638: E/System(51):   at android.os.BinderProxy.transact(Native Method)
08-06 13:23:50.638: E/System(51):   at     android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
08-06 13:23:50.638: E/System(51):   at android.os.ServiceManager.addService(ServiceManager.java:72)
08-06 13:23:50.638: E/System(51):   at com.android.server.ServerThread.run(SystemServer.java:176)
08-06 13:23:50.638: E/AndroidRuntime(51): Crash logging skipped, no checkin service
08-06 13:23:56.035: E/ActivityThread(95): Failed to find provider info for android.server.checkin
08-06 13:23:56.774: E/ActivityThread(95): Failed to find provider info for android.server.checkin
08-06 13:23:56.844: E/ActivityThread(95): Failed to find provider info for android.server.checkin
08-06 13:23:58.464: E/vold(26): Cannot start volume '/sdcard' (volume is not bound)
08-06 13:23:58.604: E/MediaPlayerService(30): Couldn't open fd for content://settings/system/notification_sound
08-06 13:23:58.614: E/MediaPlayer(51): Unable to to create media player
08-06 13:24:03.963: E/AndroidRuntime(192): ERROR: thread attach failed
08-06 13:24:05.833: E/AndroidRuntime(203): ERROR: thread attach failed
08-06 13:24:06.043: E/jdwp(216): Failed sending req to debugger: Broken pipe (-1 of 27)
08-06 13:24:06.043: E/jdwp(216): Failed sending req to debugger: Broken pipe (-1 of 27)
08-06 13:24:06.043: E/jdwp(216): Failed sending reply to debugger: Broken pipe
08-06 13:24:06.115: E/AndroidRuntime(216): Uncaught handler: thread main exiting due to uncaught exception
08-06 13:24:06.133: E/AndroidRuntime(216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deadEddie.counter/com.deadEddie.counter.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.os.Looper.loop(Looper.java:123)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-06 13:24:06.133: E/AndroidRuntime(216):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:24:06.133: E/AndroidRuntime(216):  at java.lang.reflect.Method.invoke(Method.java:521)
08-06 13:24:06.133: E/AndroidRuntime(216):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-06 13:24:06.133: E/AndroidRuntime(216):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-06 13:24:06.133: E/AndroidRuntime(216):  at dalvik.system.NativeStart.main(Native Method)
08-06 13:24:06.133: E/AndroidRuntime(216): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
08-06 13:24:06.133: E/AndroidRuntime(216):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-06 13:24:06.133: E/AndroidRuntime(216):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.Activity.setContentView(Activity.java:1622)
08-06 13:24:06.133: E/AndroidRuntime(216):  at com.deadEddie.counter.MainActivity.onCreate(MainActivity.java:20)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 08-06 13:24:06.133: E/AndroidRuntime(216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

 08-06 13:24:06.133: E/AndroidRuntime(216):     ... 11 more
08-06 13:24:06.133: E/AndroidRuntime(216): Caused by: java.lang.reflect.InvocationTargetException
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.widget.TextView.<init>(TextView.java:320)
08-06 13:24:06.133: E/AndroidRuntime(216):  at java.lang.reflect.Constructor.constructNative(Native Method)
08-06 13:24:06.133: E/AndroidRuntime(216):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
08-06 13:24:06.133: E/AndroidRuntime(216):  ... 22 more
08-06 13:24:06.133: E/AndroidRuntime(216): Caused by: android.content.res.Resources$NotFoundException: File #CCFF00 from drawable resource ID #0x7f040006: .xml extension required
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.content.res.Resources.loadColorStateList(Resources.java:1820)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.content.res.TypedArray.getColorStateList(TypedArray.java:289)
08-06 13:24:06.133: E/AndroidRuntime(216):  at android.widget.TextView.<init>(TextView.java:627)
08-06 13:24:06.133: E/AndroidRuntime(216):  ... 26 more
08-06 13:24:06.173: E/dalvikvm(216): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
08-06 13:29:06.424: E/gralloc(51): [unregister] handle 0x52d470 still locked (state=40000001)
4

2 回答 2

3

我认为您需要将颜色声明移出 strings.xml 并将其放入 colors.xml

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myColor">#ccff00</color>
</resources>

然后在您的布局中像这样引用它:

<TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/myColor"
android:textSize="30dp"
android:text="@string/text"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/tvDisplay"

/>
于 2012-08-06T14:18:33.537 回答
2

在您的 xml 中,这一行是错误的:

android:textColor="@string/color

您应该检查您是否有一个名为“/res/color/color.xml”的文件,其中包含一些对颜色的引用。

或者只是改变:

android:textColor="@string/color

到 android:textColor="#ff000000"

于 2012-08-06T14:18:29.710 回答