我成功設法將我的佈局變成位圖。然後我想要做的是使用我已經設置的Renderscript模糊方法來模糊Bitmap,但是當試圖模糊位圖時,我一直在收到NullPointerException。NPE模糊佈局位圖
當我將Renderscripy blurr方法傳遞給其他位圖時,它可以工作,但是當我從我的佈局中傳遞我創建的位圖時,它不起作用。
有人會知道我在做什麼錯嗎?這真讓我抓狂。
RelativeLayout blurlayout;
blurlayout = (RelativeLayout) view.findViewById(R.id.blurcontainer);
blurlayout.setDrawingCacheEnabled(true);
blurlayout.buildDrawingCache();
Bitmap bluredlayout = blurlayout.getDrawingCache();
//This returns null
Bitmap finalBlur = Bitmap.createBitmap(fastblur16(bluredlayout, 25, getActivity()));
Drawable d = new BitmapDrawable(getResources(), finalBlur);
private static Bitmap fastblur16(Bitmap source, int radius, Context ctx) {
Bitmap bitmap = source.copy(source.getConfig(), true);
RenderScript rs = RenderScript.create(ctx);
Allocation input = Allocation.createFromBitmap(rs, source,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs,
Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}
堆棧跟蹤:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.stackoverflowquestion.FragmentView.fastblur16(FragmentView.java:221)
at com.example.stackoverflowquestion.FragmentView.onCreateView(FragmentView.java:178)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
at android.view.View.measure(View.java:16030)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
使用@Katerina A.解決方案:
我在onCreateView此代碼對我的片段
blurlayout = (RelativeLayout) view.findViewById(R.id.blurcontainer);
blurlayout.setDrawingCacheEnabled(true);
blurlayout.buildDrawingCache(true);
drawnCacheBittmap = Bitmap.createBitmap(blurlayout.getDrawingCache());
blurlayout.setDrawingCacheEnabled(false);
然後我試圖使用onStart
@Override
public void onStart() {
// TODO Auto-generated method stub
Bitmap bluredlayout = Bitmap.createBitmap(fastblur16(drawnCacheBitmap, 25,
getActivity()));
Drawable d = new BitmapDrawable(getResources(), bluredlayout);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
blurlayout.setBackgroundDrawable(d);
} else {
blurlayout.setBackground(d);
}
super.onStart();
}
請發佈堆棧跟蹤 – MemLeak 2014-11-04 12:19:19
@MemLeak我已更新。 – Jack 2014-11-04 12:23:00
你確定你的bluredlayout位圖不是null嗎? – 2014-11-04 12:35:21