java - 如何在 Android 应用程序中创建自定义 View ?

标签 java android android-layout android-view

我已经尝试创建自定义 View 3 天了。我已经按照每一个教程进行操作,但我不断收到错误“ClassNotFoundException”。我想做一个操纵杆,但为了简化这个问题,我尝试扩展一个 Button 并使用它,但我无法成功。

主要 Activity .java:

package com.stefan.example;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    public class CustomButton extends Button
    {
        public CustomButton(Context context) {
            super(context);
        }

        public CustomButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    }

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

acitivity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.stefan.example.MainActivity">

    <com.stefan.example.MainActivity.CustomButton
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

它根本不会在编译时提示,但是一旦我启动该应用程序,它就会崩溃并出现以下错误:

10-14 17:31:58.476 15869-15869/com.stefan.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.stefan.example, PID: 15869
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stefan.example/com.stefan.example.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.stefan.example.MainActivity.CustomButton
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:149)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5297)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                     Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.stefan.example.MainActivity.CustomButton
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                        at com.stefan.example.MainActivity.onCreate(MainActivity.java:30)
                                                                        at android.app.Activity.performCreate(Activity.java:6020)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:149) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5297) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
                                                                     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.stefan.example.MainActivity.CustomButton" on path: DexPathList[[zip file "/data/app/com.stefan.example-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                        at android.view.LayoutInflater.createView(LayoutInflater.java:571)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                        at com.stefan.example.MainActivity.onCreate(MainActivity.java:30) 
                                                                        at android.app.Activity.performCreate(Activity.java:6020) 
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:149) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5297) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
                                                                        Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.stefan.example.MainActivity.CustomButton" on path: DexPathList[[dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-support-annotations-24.1.1_6ce5cf7d8fc68e278012c1fdb63847d6984b1a82-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-internal_impl-24.1.1_d7334ac7335287759c12e17e515a73749d4ea418-classes.dex", dex file "/data/data/com.stefan.example/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.1.1_e21575ccf9da39363d36f579d76036b9825e0829-classes.dex

我试过更改 xml 文件:

<Button
        class = "com.stefan.example.MainActivity.CustomButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

它可以运行,但 View 的行为就像一个普通按钮,而且我的类未被使用。

我已经在不同的设备上试用过该应用程序;都给出相同的异常(exception)。

我是不是忘记了什么?我希望我只是犯了一个简单的错误。 提前致谢!

最佳答案

要么将您的 CustomButton 类移动到顶层,要么使其成为staticLayoutInflater 无法实例化您的类,因为它是一个非静态内部类。

关于java - 如何在 Android 应用程序中创建自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048228/

相关文章:

java - 使用滑动手势向上/向下移动对象

java - mkdir() 没有创建新目录

Java 等待/通知全部

java - 如何在 Java GRPC 中配置 .proto 路径?

android - 平台和设置说明?

android - react-native 获取当前日期

Ubuntu Linux 上的 Java 崩溃

android - NDK : how to build a lib, 以便应用程序可以在 arm(s)、x86 等上运行?

android - 如何从 1 个地方记录生产构建中的崩溃报告

Android 在水平和垂直方向上适合所有设备布局的正确方法