Android自定义用户界面实现错误

时间:2014-07-14 22:29:25

标签: java android user-interface

我遵循本教程:http://developer.android.com/training/custom-views/create-view.html

并且无法弄清楚为什么我的应用程序无法正常运行...应用程序编译并运行没有问题,但随后它立即关闭并出现此类错误:

07-14 18:06:51.617: I/ActivityManager(383): Start proc com.example.customui for activity com.example.customui/.MainActivity: pid=4741 uid=10056 gids={50056}
07-14 18:06:51.647: D/dalvikvm(4741): Not late-enabling CheckJNI (already on)
07-14 18:06:52.607: D/AndroidRuntime(4741): Shutting down VM
07-14 18:06:52.607: W/dalvikvm(4741): threadid=1: thread exiting with uncaught exception (group=0xb2a83ba8)
07-14 18:06:52.617: E/AndroidRuntime(4741): FATAL EXCEPTION: main
07-14 18:06:52.617: E/AndroidRuntime(4741): Process: com.example.customui, PID: 4741
07-14 18:06:52.617: E/AndroidRuntime(4741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customui/com.example.customui.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class schemas.android.com.example.customui.PieChart
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.os.Looper.loop(Looper.java:136)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at java.lang.reflect.Method.invoke(Method.java:515)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at dalvik.system.NativeStart.main(Native Method)
07-14 18:06:52.617: E/AndroidRuntime(4741): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class schemas.android.com.example.customui.PieChart
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.Activity.setContentView(Activity.java:1929)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at com.example.customui.MainActivity.onCreate(MainActivity.java:11)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.Activity.performCreate(Activity.java:5231)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-14 18:06:52.617: E/AndroidRuntime(4741):     ... 11 more
07-14 18:06:52.617: E/AndroidRuntime(4741): Caused by: java.lang.ClassNotFoundException: Didn't find class "schemas.android.com.example.customui.PieChart" on path: DexPathList[[zip file "/data/app/com.example.customui-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.customui-1, /system/lib]]
07-14 18:06:52.617: E/AndroidRuntime(4741):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
07-14 18:06:52.617: E/AndroidRuntime(4741):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
07-14 18:06:52.617: E/AndroidRuntime(4741):     ... 21 more
07-14 18:06:52.637: W/ActivityManager(383):   Force finishing activity com.example.customui/.MainActivity
07-14 18:06:52.957: I/WindowManager(383): Screenshot max retries 4 of Token{b3031120 ActivityRecord{b2f8ee60 u0 com.example.customui/.MainActivity t69 f}} appWin=Window{b2d5cc10 u0 Starting com.example.customui} drawState=4
07-14 18:06:52.957: W/WindowManager(383): Screenshot failure taking screenshot for (1024x600) to layer 21015
07-14 18:06:53.487: W/ActivityManager(383): Activity pause timeout for ActivityRecord{b2f8ee60 u0 com.example.customui/.MainActivity t69 f}
07-14 18:06:53.637: W/EGL_emulation(542): eglSurfaceAttrib not implemented
07-14 18:06:54.787: I/Process(4741): Sending signal. PID: 4741 SIG: 9
07-14 18:06:54.807: E/SoundPool(383): error loading /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.807: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.807: E/SoundPool(383): error loading /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.807: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.827: E/SoundPool(383): error loading /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.837: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.837: E/SoundPool(383): error loading /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.837: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.837: E/SoundPool(383): error loading /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.837: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-14 18:06:54.867: E/SoundPool(383): error loading /system/media/audio/ui/KeypressStandard.ogg
07-14 18:06:54.877: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-14 18:06:54.877: E/SoundPool(383): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-14 18:06:54.887: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-14 18:06:54.887: E/SoundPool(383): error loading /system/media/audio/ui/KeypressDelete.ogg
07-14 18:06:54.887: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-14 18:06:54.887: E/SoundPool(383): error loading /system/media/audio/ui/KeypressReturn.ogg
07-14 18:06:54.887: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-14 18:06:54.887: E/SoundPool(383): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-14 18:06:54.887: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-14 18:06:54.887: W/AudioService(383): onLoadSoundEffects(), Error -1 while loading samples
07-14 18:06:55.047: W/InputMethodManagerService(383): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b2fd28e8 attribute=null, token = android.os.BinderProxy@b2fac040
07-14 18:06:55.187: D/LightsService(383): Excessive delay setting light: 449ms
07-14 18:06:56.017: I/ActivityManager(383): Process com.example.customui (pid 4741) has died.
07-14 18:07:49.167: D/dalvikvm(649): GC_FOR_ALLOC freed 552K, 75% free 4347K/17120K, paused 64ms, total 67ms
07-14 18:15:00.997: D/ConnectivityService(383): Sampling interval elapsed, updating statistics ..
07-14 18:15:01.007: D/ConnectivityService(383): Done.
07-14 18:15:01.017: D/ConnectivityService(383): Setting timer for 720seconds
07-14 18:19:50.587: D/MDnsDS(49): MDnsSdListener::Monitor poll timed out
07-14 18:19:50.587: D/MDnsDS(49): Going to poll with pollCount 1
07-14 18:20:30.407: D/dalvikvm(533): GC_FOR_ALLOC freed 589K, 18% free 3241K/3920K, paused 54ms, total 55ms

这是我的课程和文件:

MainActivity.java:

package com.example.customui;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

PieChart.java:

package com.example.customui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

class PieChart extends View {
    boolean mShowText;
    int mTextPos;
    public PieChart(Context context, AttributeSet attrs) {
           super(context, attrs);
           TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.PieChart,
                0, 0);

           try {
               mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
               mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
           } finally {
               a.recycle();
           }
        }
    public boolean isShowText() {
           return mShowText;
        }

        public void setShowText(boolean showText) {
           mShowText = showText;
           invalidate();
           requestLayout();
        }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customui"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
 <com.example.customui.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

attrs.xml:

<resources>
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>

MainActivity.java和PieChart.java位于com.example.customui包中,其他文件也位于其默认位置。 我无法弄清楚问题是什么,因为我已经完成了教程中规定的几乎所有内容......我将非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以尝试使用activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res-auto"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
 <com.example.customui.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

答案 1 :(得分:1)

尝试更改

的xmlns:定制=&#34; HTTP://schemas.android.com/apk/res/com.example.customui"

的xmlns:定制=&#34; HTTP://schemas.android.com/apk/res-auto"

这就是所有人。