Android custom-view - java.lang.RuntimeException android.view.InflateException

时间:2014-11-26 23:23:04

标签: java android android-widget custom-view

创建窗口小部件时遇到问题。我是Android Studio v1.0的使用者。 首先,我选择遵循这些线条: http://developer.android.com/training/custom-views/create-view.html 这就是我所做的: activity_home.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:TAG="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Home">

    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        />

    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/write"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        />

    <com.stecape.pimp.Home.TAG
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText1"
        android:layout_below="@+id/write"
        android:layout_alignParentLeft="true"
        TAG:structure="DB"
        TAG:DBnumber="1"
        TAG:datatype="BIT"
        TAG:address="0.1" />

</RelativeLayout>

这会导致在屏幕的实时预览部分显示此警告:

Rendering Problems The following classes could not be instantiated:
- com.stecape.pimp.Home.TAG (Open Class, Show Exception)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. 
Exeption details:
java.lang.NoSuchMethodException: com.stecape.pimp.Home$TAG.<init>(android.content.Context, android.util.AttributeSet)
at java.lang.Class.getConstructor0(Class.java:3069)
at java.lang.Class.getConstructor(Class.java:1812)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:314)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:99)
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:172)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
at android.view.LayoutInflater.inflate(LayoutInflater.java:381)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:329)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:333)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:674)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:663)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:663)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:790)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:611)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.java:81)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:553)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:548)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

这是保存在值中的attr.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TAG">
        <attr name="structure" format="enum">
            <enum name="M" value="0"/>
            <enum name="DB" value="1"/>
        </attr>
        <attr name="DBnumber" format="integer"/>
        <attr name="datatype" format="enum">
            <enum name="BIT" value="0"/>
            <enum name="BYTE" value="1"/>
            <enum name="WORD" value="2"/>
            <enum name="DOUBLE" value="4"/>
        </attr>
        <attr name="address" format="string"/>

    </declare-styleable>
</resources>

最后,这里有我的Home.java:

package com.stecape.pimp;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Home extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class TAG extends View {
        public TAG(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    }
}

应用程序编译时没有任何问题,但是当我在手机上运行它(samsung galaxy s3 API18)它会崩溃并给出这个错误:

11-26 23:56:31.455    9826-9826/com.stecape.pimp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stecape.pimp/com.stecape.pimp.Home}: android.view.InflateException: Binary XML file line #31: Error inflating class com.stecape.pimp.Home.TAG
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.stecape.pimp.Home.TAG
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
        at android.app.Activity.setContentView(Activity.java:1956)
        at com.stecape.pimp.Home.onCreate(Home.java:35)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.ClassNotFoundException: Didn't find class "com.stecape.pimp.Home.TAG" on path: /data/app/com.stecape.pimp-1.apk
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        at android.view.LayoutInflater.createView(LayoutInflater.java:565)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
        at android.app.Activity.setContentView(Activity.java:1956)
        at com.stecape.pimp.Home.onCreate(Home.java:35)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

我真的不知道我哪里错了。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

让您的TAG类保持静态。由于它是一个内部类,如果你使它成为非静态类,那么编译器会向外部Activity类添加一个隐藏引用。这就是你得到NoSuchMethodException的原因。

public static class TAG extends View {
    public TAG(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

此外,当您在XML中引用此类时,您需要使用&#34; $ TAG&#34;不是&#34; .TAG&#34;因为它是一个内在的阶级。这就是你得到ClassNotFoundException的原因。

<view class="com.stecape.pimp.Home$TAG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText1"
    android:layout_below="@+id/write"
    android:layout_alignParentLeft="true"
    TAG:structure="DB"
    TAG:DBnumber="1"
    TAG:datatype="BIT"
    TAG:address="0.1" />