简体   繁体   English

奇怪的Android Studio行为

[英]Strange Android Studio behavior

I'm developing app for image editing and for some reasons something go wrong. 我正在开发用于图像编辑的应用程序,由于某些原因,出现了问题。 App started to throw error 应用开始引发错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ekalips/com.example.ekalips.EditorActivity}: java.lang.ClassCastException: android.opengl.GLSurfaceView cannot be cast to android.widget.ImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.opengl.GLSurfaceView cannot be cast to android.widget.ImageView
    at com.example.ekalips.EditorActivity.onCreate(EditorActivity.java:31)
    at android.app.Activity.performCreate(Activity.java:6251)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                           

But I'm not really trying to convert. 但是我并不是真的想转换。 Here's activity code: 这是活动代码:

public class EditorActivity extends AppCompatActivity implements GLSurfaceView.Renderer{
    private boolean isFullScreen = false;
    PhotoViewAttacher attacher;
    RecyclerView recyclerView;
    Context context;

    private GLSurfaceView mEffectView;
    private int[] mTextures = new int[2];
    private EffectContext mEffectContext;
    private Effect mEffect;
    private TextureRenderer mTexRenderer = new TextureRenderer();
    private int mImageWidth;
    private int mImageHeight;
    private boolean mInitialized = false;
    int mCurrentEffect;
    Uri uri;
    Filter[] filterColl = new Filter[24];
    public void setCurrentEffect(int effect) {
        mCurrentEffect = effect;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor);
        context = this;

        uri = Uri.parse(getIntent().getStringExtra("urilol"));
        mEffectView = (GLSurfaceView) findViewById(R.id.effectsview);
        mEffectView.setEGLContextClientVersion(2);
        mEffectView.setRenderer(this);
        mEffectView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        mCurrentEffect = R.id.none;

        final ImageButton imageButton = (ImageButton) findViewById(R.id.menuBtn);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup(v);
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(layoutManager);


        filterColl[0] = new Filter(0,"None");
        filterColl[1] =new Filter(1,"Autofix");
        filterColl[2] =new Filter(2,"Min/Max Color Intensity");
        filterColl[3] =new Filter(3,"Brightness");
        filterColl[4] =new Filter(4,"Contrast");
        filterColl[5] =new Filter(5,"Cross Process");
        filterColl[6] =new Filter(6,"Documentary");
        filterColl[7] =new Filter(7,"Duo Tone");
        filterColl[8] =new Filter(8,"Fill Light");
        filterColl[9] =new Filter(9,"Fish Eye");
        filterColl[10] =new Filter(10,"Flip Vertical");
        filterColl[11] =new Filter(11,"Flip Horizontal");
        filterColl[12] =new Filter(12,"Grain");
        filterColl[13] =new Filter(13,"Grayscale");
        filterColl[14] =new Filter(14,"Lomoish");
        filterColl[15] =new Filter(15,"Negative");
        filterColl[16] =new Filter(16,"Posterize");
        filterColl[17] =new Filter(17,"Rotate");
        filterColl[18] =new Filter(18,"Saturate");
        filterColl[19] =new Filter(19,"Sepia");
        filterColl[20] =new Filter(20,"Sharpen");
        filterColl[21] =new Filter(21,"Temperature");
        filterColl[22] =new Filter(22,"Tint");
        filterColl[23] =new Filter(23,"Vignette");

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(filterColl);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        TextView textView = (TextView) view.findViewById(R.id.itemText);
                        Log.d("lele",textView.getText().toString() + "text");
                          mEffectView.requestRender();
                    }
                })
        );


    }


    public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.edit_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                setCurrentEffect(item.getItemId());
                mEffectView.requestRender();
                return true;
            }
        });
        popup.show();
    }

    private void loadTextures() {
        // Generate textures
        GLES20.glGenTextures(2, mTextures, 0);

        // Load input bitmap
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }

        mImageWidth = bitmap.getWidth();
        mImageHeight = bitmap.getHeight();
        mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);

        // Upload to texture
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Set texture parameters
        GLToolbox.initTexParams();
    }

    private void initEffect() {
        EffectFactory effectFactory = mEffectContext.getFactory();
        if (mEffect != null) {
            mEffect.release();
        }
        /**
         * Initialize the correct effect based on the selected menu/action item
         */
        switch (mCurrentEffect) {

            case R.id.none:
                break;

            case R.id.autofix:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_AUTOFIX);
                mEffect.setParameter("scale", 0.5f);
                break;

            case R.id.bw:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_BLACKWHITE);
                mEffect.setParameter("black", .1f);
                mEffect.setParameter("white", .7f);
                break;

            case R.id.brightness:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_BRIGHTNESS);
                mEffect.setParameter("brightness", 2.0f);
                break;

            case R.id.contrast:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_CONTRAST);
                mEffect.setParameter("contrast", 1.4f);
                break;

            case R.id.crossprocess:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_CROSSPROCESS);
                break;

            case R.id.documentary:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_DOCUMENTARY);
                break;

            case R.id.duotone:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_DUOTONE);
                mEffect.setParameter("first_color", Color.YELLOW);
                mEffect.setParameter("second_color", Color.DKGRAY);
                break;

            case R.id.filllight:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_FILLLIGHT);
                mEffect.setParameter("strength", .8f);
                break;

            case R.id.fisheye:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_FISHEYE);
                mEffect.setParameter("scale", .5f);
                break;

            case R.id.flipvert:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_FLIP);
                mEffect.setParameter("vertical", true);
                break;

            case R.id.fliphor:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_FLIP);
                mEffect.setParameter("horizontal", true);
                break;

            case R.id.grain:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_GRAIN);
                mEffect.setParameter("strength", 1.0f);
                break;

            case R.id.grayscale:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_GRAYSCALE);
                break;

            case R.id.lomoish:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_LOMOISH);
                break;

            case R.id.negative:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_NEGATIVE);
                break;

            case R.id.posterize:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_POSTERIZE);
                break;

            case R.id.rotate:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_ROTATE);
                mEffect.setParameter("angle", 180);
                break;

            case R.id.saturate:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_SATURATE);
                mEffect.setParameter("scale", .5f);
                break;

            case R.id.sepia:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_SEPIA);
                break;

            case R.id.sharpen:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_SHARPEN);
                break;

            case R.id.temperature:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_TEMPERATURE);
                mEffect.setParameter("scale", .9f);
                break;

            case R.id.tint:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_TINT);
                mEffect.setParameter("tint", Color.MAGENTA);
                break;

            case R.id.vignette:
                mEffect = effectFactory.createEffect(
                        EffectFactory.EFFECT_VIGNETTE);
                mEffect.setParameter("scale", .5f);
                break;

            default:
                break;

        }
    }
    private void applyEffect() {
        mEffect.apply(mTextures[0], mImageWidth, mImageHeight, mTextures[1]);
    }

    private void renderResult() {
        if (mCurrentEffect != R.id.none) {
            // if no effect is chosen, just render the original bitmap
            mTexRenderer.renderTexture(mTextures[1]);
        }
        else {
            // render the result of applyEffect()
            mTexRenderer.renderTexture(mTextures[0]);
        }
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (mTexRenderer != null) {
            mTexRenderer.updateViewSize(width, height);
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        if (!mInitialized) {
            //Only need to do this once
            mEffectContext = EffectContext.createWithCurrentGlContext();
            mTexRenderer.init();
            loadTextures();
            mInitialized = true;
        }
        if (mCurrentEffect != R.id.none) {
            //if an effect is chosen initialize it and apply it to the texture
            initEffect();
            applyEffect();
        }
        renderResult();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        setCurrentEffect(item.getItemId());
        mEffectView.requestRender();
        return true;
    }
}

And here is activity xml code: 这是活动xml代码:

 <RelativeLayout 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"
        tools:context="com.example.djqrj.allah.EditorActivity">
        <android.opengl.GLSurfaceView
            android:id="@+id/effectsview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menuBtn"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>
        <android.support.v7.widget.RecyclerView
            android:clickable="true"
            android:focusable="true"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:id="@+id/recyclerView"
            android:background="#B3000000"
            android:layout_alignParentBottom="true"
            android:layout_alignEnd="@+id/menuBtn"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true" />
</RelativeLayout>

I tried to Build - Clean/Rebuild Project,deleting R.class file but it didn't help. 我试图构建-清理/重建项目,删除R.class文件,但没有帮助。

Also before this error I had another one - when I tried to add app rejected to start because NullPointerException in onClickListener (java code wasn't changed, but error appeared when adding ). 另外这个错误之前,我有另外一个-当我试图添加应用程序被拒绝启动,因为NullPointerExceptiononClickListener (java代码没有改变,但增加时出现的错误)。

Thanks for help. 感谢帮助。

The error states that the code running on your device (or emulator), on line 31 of EditorActivity.java in the onCreate() method, you are trying to cast to ImageView but what you have is a GLSurfaceView. 该错误表明在设备(或仿真器)上运行的代码,在onCreate()方法中EditorActivity.java的第31行上,您尝试转换为ImageView,但您拥有的是GLSurfaceView。

 Caused by: java.lang.ClassCastException: android.opengl.GLSurfaceView cannot be cast to android.widget.ImageView at com.example.ekalips.EditorActivity.onCreate(EditorActivity.java:31) 

Since the code you posted doesn't have any references to ImageView my guess is that the code you are looking at is not the code you are running. 由于您发布的代码没有对ImageView的任何引用,我想您正在查看的代码不是您正在运行的代码。 Clean, rebuild, and then be sure to install the newly built app on the device. 清理,重建,然后确保在设备上安装了新构建的应用程序。 Make sure the installation of the new app doesn't fail leaving you with an old version running on the device. 确保新应用程序的安装不会失败,使您可以在设备上运行旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM