如何摆脱这个??? “无法实例化以下类”
Posted
技术标签:
【中文标题】如何摆脱这个??? “无法实例化以下类”【英文标题】:How to get rid of this ??? "The following classes could not be instantiated" 【发布时间】:2012-08-27 08:28:42 【问题描述】:这是我的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@drawable/bgcategory"
android:orientation="vertical" >
<RelativeLayout
android:layout_
android:layout_ >
<ImageView
android:id="@+id/img_top"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/topbar" >
</ImageView>
<ImageView
android:id="@+id/img_back"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:src="@drawable/btn_back" >
</ImageView>
<ImageView
android:id="@+id/img_share"
android:layout_
android:layout_
android:layout_alignBottom="@+id/img_back"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:src="@drawable/img_share">
</ImageView>
<TextView
android:id="@+id/txt_audio_title"
android:layout_
android:layout_
android:layout_alignBottom="@+id/btn_share"
android:layout_alignTop="@+id/img_back"
android:layout_toLeftOf="@+id/img_share"
android:layout_toRightOf="@+id/img_back"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text=""
android:textColor="#fffafa"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
<ScrollView
android:id="@+id/scrollview"
android:layout_
android:layout_>
<RelativeLayout
android:layout_
android:layout_
android:orientation="vertical" >
<com.example.activities.VisualizerView
android:id="@+id/visualizerView_scroll"
android:layout_
android:layout_
android:layout_marginTop="10dip">
</com.example.activities.VisualizerView>
<SeekBar
android:id="@+id/seekbar"
android:layout_
android:layout_
android:layout_below="@id/visualizerView_scroll"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:scaleType="fitXY" >
</SeekBar>
<ImageView
android:id="@+id/play"
android:layout_
android:layout_
android:layout_below="@+id/seekbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dip"
android:scaleType="fitXY"
android:src="@drawable/new_play" >
</ImageView>
<ImageView
android:id="@+id/pause"
android:layout_
android:layout_
android:layout_alignTop="@+id/play_scroll"
android:layout_toLeftOf="@+id/play_scroll"
android:scaleType="fitXY"
android:src="@drawable/new_pause" />
<ImageView
android:id="@+id/stop"
android:layout_
android:layout_
android:layout_alignTop="@+id/play_scroll"
android:layout_toRightOf="@+id/play_scroll"
android:scaleType="fitXY"
android:src="@drawable/new_stop" />
<LinearLayout
android:id="@+id/llCommentContainer"
android:layout_
android:layout_
android:orientation="vertical"
android:layout_below="@+id/play_scroll">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@+id/llCommentContainer">
<ImageView
android:id="@+id/img_comment"
android:layout_
android:layout_
android:layout_gravity="right"
android:layout_alignParentBottom="true"
android:clickable="true"
android:src="@drawable/btn_addcomment">
</ImageView>
</RelativeLayout>
这是类 ::
public class VisualizerView extends View
private static final String TAG = "VisualizerView";
private byte[] mBytes;
private byte[] mFFTBytes;
private Rect mRect = new Rect();
private Visualizer mVisualizer;
private Set<Renderer> mRenderers;
private Paint mFlashPaint = new Paint();
private Paint mFadePaint = new Paint();
public VisualizerView(Context context, AttributeSet attrs, int defStyle)
super(context, attrs);
init();
public VisualizerView(Context context, AttributeSet attrs)
this(context, attrs, 0);
public VisualizerView(Context context)
this(context, null, 0);
public void init()
mBytes = null;
mFFTBytes = null;
mFlashPaint.setColor(Color.argb(122, 255, 255, 255));
mFadePaint.setColor(Color.argb(238, 255, 255, 255)); // Adjust alpha to change how quickly the image fades
mFadePaint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
mRenderers = new HashSet<Renderer>();
/**
* Links the visualizer to a player
* @param player - MediaPlayer instance to link to
*/
public void link(MediaPlayer player)
if(player == null)
throw new NullPointerException("Cannot link to null MediaPlayer");
// Create the Visualizer object and attach it to our media player.
mVisualizer = new Visualizer(player.getAudiosessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
// Pass through Visualizer data to VisualizerView
Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener()
//@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,int samplingRate)
updateVisualizer(bytes);
//@Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,int samplingRate)
updateVisualizerFFT(bytes);
;
mVisualizer.setDataCaptureListener(captureListener,
Visualizer.getMaxCaptureRate() / 2, true, true);
// Enabled Visualizer and disable when we're done with the stream
mVisualizer.setEnabled(true);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
//@Override
public void onCompletion(MediaPlayer mediaPlayer)
mVisualizer.setEnabled(false);
);
public void addRenderer(Renderer renderer)
if(renderer != null)
mRenderers.add(renderer);
public void clearRenderers()
mRenderers.clear();
/**
* Call to release the resources used by VisualizerView. Like with the
* MediaPlayer it is good practice to call this method
*/
public void release()
mVisualizer.release();
/**
* Pass data to the visualizer. Typically this will be obtained from the
* Android Visualizer.OnDataCaptureListener call back. See
* @link Visualizer.OnDataCaptureListener#onWaveFormDataCapture
* @param bytes
*/
public void updateVisualizer(byte[] bytes)
mBytes = bytes;
invalidate();
/**
* Pass FFT data to the visualizer. Typically this will be obtained from the
* Android Visualizer.OnDataCaptureListener call back. See
* @link Visualizer.OnDataCaptureListener#onFftDataCapture
* @param bytes
*/
public void updateVisualizerFFT(byte[] bytes)
mFFTBytes = bytes;
invalidate();
boolean mFlash = false;
/**
* Call this to make the visualizer flash. Useful for flashing at the start
* of a song/loop etc...
*/
public void flash()
mFlash = true;
invalidate();
Bitmap mCanvasBitmap;
Canvas mCanvas;
@Override
protected void onDraw(Canvas canvas)
super.onDraw(canvas);
// Create canvas once we're ready to draw
mRect.set(0, 0, getWidth(), getHeight());
if(mCanvasBitmap == null)
mCanvasBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
if(mCanvas == null)
mCanvas = new Canvas(mCanvasBitmap);
if (mBytes != null)
// Render all audio renderers
AudioData audioData = new AudioData(mBytes);
for(Renderer r : mRenderers)
r.render(mCanvas, audioData, mRect);
if (mFFTBytes != null)
// Render all FFT renderers
FFTData fftData = new FFTData(mFFTBytes);
for(Renderer r : mRenderers)
r.render(mCanvas, fftData, mRect);
// Fade out old contents
mCanvas.drawPaint(mFadePaint);
if(mFlash)
mFlash = false;
mCanvas.drawPaint(mFlashPaint);
canvas.drawBitmap(mCanvasBitmap, new Matrix(), null);
我的错误日志 :::
java.lang.ClassNotFoundException: android.media.audiofx.Visualizer$OnDataCaptureListener
at com.android.ide.eclipse.adt.internal.resources.manager.ProjectClassLoader.findClass(ProjectClassLoader.java:105)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2406)
at java.lang.Class.getConstructor0(Class.java:2716)
at java.lang.Class.getConstructor(Class.java:1674)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:327)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:166)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:739)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:321)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:322)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1640)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1391)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1165)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:679)
at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:359)
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:292)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1079)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3023)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1730)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:270)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3554)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3179)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1022)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:916)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:585)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:540)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
现在,当用户单击播放按钮时,我正在尝试使用此可视化视图。 但是在我的文件图形布局中,我收到“无法实例化以下类”的消息,我也无法在视图中显示我的 imageview(即 imgComment)。有什么建议我错了吗? ?
在此先感谢....
【问题讨论】:
你能告诉我们哪个类不能被实例化(你的错误信息的第二行)?此外,您的日志窗口的副本可能会很有用。最后,您的布局有一些错误(一个空且无用的 LinearLayout,一个 @layout_below 属性引用另一个位于不同 RelativeLayout 父级中的视图,可能还有其他...) @Orabig 我已经编辑了我的问题..请看一下:-) 【参考方案1】:我的代码也遇到了同样的问题。从我的自定义视图中取出对 Android Visualizer 类的任何引用并将其放入我的活动类之后,一切都很好了。 (基本上这是 Orabîg 在他的回复中推荐的,但我无法对此发表评论。)
但是,这是一个应该报告给 ADT 的错误吗?
【讨论】:
【参考方案2】:由于“ClassNotFound”异常,您的自定义视图 (com.example.activities.VisualizerView) 无法在 Eclipse 中加载。 我猜 Eclipse 布局查看器插件不包含 android.media.audiofx 包...
也许您可以尝试将这段代码(link() 方法)外部化到一个单独的类中,看看 Eclipse 是否更快乐并能够成功加载这个类?
【讨论】:
以上是关于如何摆脱这个??? “无法实例化以下类”的主要内容,如果未能解决你的问题,请参考以下文章
Android:Eclipse:无法实例化以下类:- com.google.android.gms.common.SignInButton
在 AppDelegate 中实例化 UINavigationController