[英]Android ClassNotFoundException after including GaugeView library
我正在嘗試使用來自github的Gauge View
該庫提供的示例應用程序非常完美!
但是,當我嘗試將庫包含到項目中(屬性> buildpath>項目>(添加庫項目))時,將視圖放入我的布局中,並嘗試在拋出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;
}
}
您沒有發布任何代碼,但是即使在您的帖子中,特定單詞的拼寫仍然不一致,這一事實表明您需要檢查代碼中的拼寫方式。 仔細看這兩個詞:
String correct = "gauge";
String wrongWrongWrong = "guage";
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>
您使用的儀表存在錯誤,在Kitkat和Lollipop上無法正常運行。 另外,這里沒有Android Studio和gradle友好庫。
這基本上是您正在使用的相同庫,但已從其他開發人員分叉,並已更新為可用於新版本的android。 您也可以與Gradle一起使用,並輕松地將其包含在項目中,而不會出現任何錯誤:
在項目中包含庫之后,將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() {}
};
}
這將實例化針並使其動畫化為隨機值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.