0

我正在尝试使用来自github
的 Gauge View 库 提供的示例应用程序完美!

但是当我尝试将库包含到项目中时(属性>构建路径>项目>(添加库项目)),将视图放在我的布局中并尝试在它抛出的代码中获取对视图的引用ClassNotFoundException

以下是我通过 logcat 得到的错误:

E/AndroidRuntime(  542): FATAL EXCEPTION: main
E/AndroidRuntime(  542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guagetest/com.example.guagetest.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class org.codeandmagic.android.gauge.GaugeView
E/AndroidRuntime(  542):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
E/AndroidRuntime(  542):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
E/AndroidRuntime(  542):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(  542):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
E/AndroidRuntime(  542):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  542):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  542):    at android.app.ActivityThread.main(ActivityThread.java:4340)
E/AndroidRuntime(  542):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  542):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  542):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(  542):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(  542):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  542): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class org.codeandmagic.android.gauge.GaugeView
E/AndroidRuntime(  542):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(  542):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
E/AndroidRuntime(  542):    at android.app.Activity.setContentView(Activity.java:1835)
E/AndroidRuntime(  542):    at com.example.guagetest.MainActivity.onCreate(MainActivity.java:16)
E/AndroidRuntime(  542):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(  542):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(  542):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
E/AndroidRuntime(  542):    ... 11 more
E/AndroidRuntime(  542): Caused by: java.lang.ClassNotFoundException: org.codeandmagic.android.gauge.GaugeView
E/AndroidRuntime(  542):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(  542):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(  542):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
E/AndroidRuntime(  542):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
E/AndroidRuntime(  542):    ... 21 more
W/ActivityManager(   77):   Force finishing activity com.example.guagetest/.MainActivity

我不知道为什么它无法找到课程。有人请帮忙。在示例应用程序中我需要做些什么才能让它工作吗?

以下是我的布局:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <org.codeandmagic.android.gauge.GaugeView
     android:id="@+id/gauge_view1"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1" />


</LinearLayout>

这是我的活动:

package com.example.guagetest;

import org.codeandmagic.android.gauge.GaugeView;

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

public class MainActivity extends Activity {

    private GaugeView gauge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gauge = (GaugeView) findViewById(R.id.gauge_view1);
        gauge.setTargetValue(10);
    }

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

}
4

3 回答 3

1

您没有发布任何代码,但即使在您的帖子中,您对特定单词的拼写也不一致这一事实表明您需要检查您在代码中的拼写方式。仔细看这两个字:

String correct = "gauge";
String wrongWrongWrong = "guage";
于 2013-05-02T11:41:25.887 回答
0

您使用的仪表有错误,并且在 Kitkat 和 Lollipop 上运行不正常。这里也没有 Android Studio 和 gradle 友好的库。

这与您使用的库基本相同,但从其他开发人员分叉并更新以适用于新版本的 android。您也可以与 Gradle 一起使用并在您的项目中轻松包含,而不会出现任何错误:

http://www.sule.io/?p=25

在此处输入图像描述

在项目中包含库后,将 gaugelibrary 添加到活动的 xml 布局中:

<io.sule.gaugelibrary.GaugeView
 android:id="@+id/gauge_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
 gauge:showOuterShadow="false"
 gauge:showOuterRim="false"
 gauge:showInnerRim="false"
 gauge:needleWidth="0.010"
 gauge:needleHeight="0.40"
 gauge:scaleStartValue="0"
 gauge:scaleEndValue="100"
 />

这将显示无针的静态量规。要使用随机动画实例化针头,您需要在活动类文件中执行此操作。看看它是如何在这里完成的:

package io.sule.testapplication;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Random;

import io.sule.gaugelibrary.GaugeView;

public class MainActivity extends Activity {
   private GaugeView mGaugeView;
   private final Random RAND = new Random();

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

      mGaugeView = (GaugeView) findViewById(R.id.gauge_view);
      mTimer.start();
   }


   private final CountDownTimer mTimer = new CountDownTimer(30000, 1000) {

      @Override
      public void onTick(final long millisUntilFinished) {
         mGaugeView.setTargetValue(RAND.nextInt(101));
      }

      @Override
      public void onFinish() {}
   };
}

这将实例化 needle 并使其动画移动到随机值。

于 2015-04-13T14:34:17.680 回答
0
public class MainActivity extends Activity {

    private GaugeView mGaugeView1;
    private GaugeView mGaugeView2;
    private final Random RAND = new Random();

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGaugeView1 = (GaugeView) findViewById(R.id.gauge_view1);
        mGaugeView2 = (GaugeView) findViewById(R.id.gauge_view2);
        mTimer.start();
    }

    private final CountDownTimer mTimer = new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(final long millisUntilFinished) {
            mGaugeView1.setTargetValue(RAND.nextInt(101));
            mGaugeView2.setTargetValue(RAND.nextInt(101));
        }

        @Override
        public void onFinish() {}
    };
}

编辑

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:gauge="http://schemas.android.com/apk/res/org.codeandmagic.android.gauge.samples"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/background"
    android:padding="20dp" >

   <org.codeandmagic.android.gauge.GaugeView
        android:id="@+id/gauge_view1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <org.codeandmagic.android.gauge.GaugeView
        android:id="@+id/gauge_view2"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="10dp"
        gauge:showOuterShadow="true"
        gauge:showOuterRim="false"
        gauge:showNeedle="false"
        gauge:showRanges="false"
        gauge:showText="true"
        gauge:textUnit="%" />

</LinearLayout>
于 2013-05-02T11:56:33.773 回答