怎么摆脱这个? “以下类无法实例化”

时间:2012-08-27 08:28:42

标签: android dynamic view

这是我的XML文件。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bgcategory"
        android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img_top"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/topbar" >
    </ImageView>

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="66dp"
        android:layout_height="40dp"
        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_width="wrap_content"
        android:layout_height="35dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.example.activities.VisualizerView
            android:id="@+id/visualizerView_scroll"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginTop="10dip">
        </com.example.activities.VisualizerView>

        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:layout_below="@+id/play_scroll">
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/llCommentContainer"> 

    <ImageView
        android:id="@+id/img_comment"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        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)。任何建议,我错了? ?

提前致谢....

2 个答案:

答案 0 :(得分:2)

我的代码遇到了同样的问题。从我的自定义视图中推出对Android Visualizer calss的任何引用并将其放入我的活动类后,一切都很好。 (基本上这是Orabîg在他的回答中推荐的,但我无法评论它。)

然而,这是一个应该向ADT报告的错误吗?

答案 1 :(得分:0)

由于“ClassNotFound”异常,您的自定义视图(com.example.activities.VisualizerView)无法在Eclipse中加载。 我猜Eclipse布局查看器插件不包含android.media.audiofx包...

也许您可以尝试在单独的类中外化此代码(link()方法)以查看Eclipse是否更高兴并且能够成功加载此类?