我正在开发用于图像编辑的应用程序,并且由于某些原因出了问题。应用程序开始抛出错误
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)
但我并没有真正尝试转换。这是活动代码:
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;
}
}
这是活动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>
我尝试构建 - 清理/重建项目,删除R.class文件,但它没有帮助。
此错误之前我还有另外一个 - 当我尝试添加被拒绝的应用时因为NullPointerException
中的onClickListener
(java代码未被更改,但添加时出现错误)。
感谢您的帮助。
答案 0 :(得分:0)
错误指出您的设备(或模拟器)上运行的代码,在onCreate()
方法的EditorActivity.java的第31行,您正在尝试转换为ImageView,但您拥有的是GLSurfaceView。 / p>
Caused by: java.lang.ClassCastException: android.opengl.GLSurfaceView cannot be cast to android.widget.ImageView at com.example.ekalips.EditorActivity.onCreate(EditorActivity.java:31)
由于你发布的代码没有对ImageView的任何引用,我猜你所看到的代码不是你正在运行的代码。清理,重建,然后确保在设备上安装新构建的应用程序。确保安装新应用程序不会失败,让您在设备上运行旧版本。