包含GaugeView库后的Android ClassNotFoundException

时间:2013-05-02 11:10:33

标签: java android mobile

我正在尝试使用github的量表视图 随库提供的示例应用程序完美无缺!

但是当我尝试将库包含到项目中时(属性> buildpath> projects>(添加库项目)),将视图放在我的布局中并尝试在它抛出的代码中获取对视图的引用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;
    }

}

3 个答案:

答案 0 :(得分:1)

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

String correct = "gauge";
String wrongWrongWrong = "guage";

答案 1 :(得分: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>

答案 2 :(得分:0)

你正在使用的量表有bug,并且在Kitkat和Lollipop上运行不正常。此处还没有Android Studio和gradle友好库。

这里基本上是你正在使用的库,但是从其他开发人员分叉并更新为适用于新版本的android。您也可以使用Gradle并在项目中轻松添加,而不会出现任何错误:

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

enter image description here

在项目中包含库后,将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() {}
   };
}

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