2015-08-18 208 views
-1
case R.id.btn_gamma: 
     GPUImage mGPUImage = new GPUImage(this); 
     mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.effectsview)); 
     mGPUImage.setImage(bm); 
     mGPUImage.setFilter(new GPUImageBrightnessFilter(4.0f)); 
     imgPreview.setImageBitmap(bm); 

XML:GLSurfaceView NullPointerException異常

<android.opengl.GLSurfaceView 
    android:id="@+id/effectsview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.05" /> 

logcat的:

08-18 16:44:39.370: E/AndroidRuntime(23398): FATAL EXCEPTION: main 
08-18 16:44:39.370: E/AndroidRuntime(23398): Process: 
com.argenit.pathology, PID: 23398 08-18 16:44:39.370: 
E/AndroidRuntime(23398): java.lang.NullPointerException 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:531) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.view.SurfaceView.updateWindow(SurfaceView.java:601) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.SurfaceView.access$000(SurfaceView.java:94) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:879) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2126) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1226) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6362) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.view.Choreographer$CallbackRecord.run(Choreographer.java:803) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.view.Choreographer.doCallbacks(Choreographer.java:603) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.Choreographer.doFrame(Choreographer.java:573) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
android.os.Handler.handleCallback(Handler.java:733) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.os.Handler.dispatchMessage(Handler.java:95) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
android.os.Looper.loop(Looper.java:157) 08-18 16:44:39.370: 
E/AndroidRuntime(23398): at 
android.app.ActivityThread.main(ActivityThread.java:5350) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
java.lang.reflect.Method.invokeNative(Native Method) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
java.lang.reflect.Method.invoke(Method.java:515) 08-18 16:44:39.370: 
E/AndroidRuntime(23398): at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
08-18 16:44:39.370: E/AndroidRuntime(23398): at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 08-18 
16:44:39.370: E/AndroidRuntime(23398): at 
dalvik.system.NativeStart.main(Native Method) 

其他信息完整的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" 
    android:orientation="vertical" 
    tools:context="com.argenit.pathology.MainActivity" > 

    <ImageView 
     android:id="@+id/imgPreview" 
     android:scaleType="matrix" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="128dp" 
     android:src="@drawable/logo" /> 

    <!-- ANA MENÜ BAŞLANGIÇ --> 
    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar_main_menu" 
     android:layout_width="fill_parent" 
     android:layout_height="64dp" 
     android:layout_marginBottom="128dp" 
     android:layout_alignParentBottom="true" 
     android:background="#cc1e252d" 
     android:minHeight="?attr/actionBarSize" > 

     <LinearLayout 
      android:id="@+id/lyt_main_menu" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:orientation="horizontal" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="60dp" 
        android:paddingRight="60dp" 
        android:id="@+id/txtDosyalar" 
        android:text="@string/dosyalar" 
        android:textColor="#68ce30" 
        android:textColorHighlight="#000000" 
        android:textSize="20sp" 
        android:clickable="true" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="60dp" 
        android:paddingRight="60dp" 
        android:id="@+id/txtObjektifler" 
        android:text="@string/objektifler" 
        android:textColor="#fff" 
        android:textColorHighlight="#000000" 
        android:textSize="20sp" 
        android:clickable="true" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="60dp" 
        android:paddingRight="60dp" 
        android:id="@+id/txtEfektler" 
        android:text="@string/efektler" 
        android:textColor="#fff" 
        android:textColorHighlight="#000000" 
        android:textSize="20sp" 
        android:clickable="true" /> 

      </LinearLayout> 

    </android.support.v7.widget.Toolbar> 
    <!-- ANA MENÜ BİTİŞ --> 


     <android.support.v7.widget.Toolbar 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar_effect_details" 
      android:layout_width="fill_parent" 
      android:layout_height="64dp" 
      android:layout_marginBottom="128dp" 
      android:layout_alignParentBottom="true" 
      android:background="#cc1e252d" 
      android:visibility="gone" 
      android:minHeight="?attr/actionBarSize" > 

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

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_closeEffect" 
         android:onClick="btn_closeEffect_Click" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:padding="10dip" 
         android:layout_alignParentLeft="true" 
         android:src="@drawable/close" 
         android:scaleType="centerInside" 
         android:adjustViewBounds="true" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/txt_effect" 
         android:textColor="#FFFFFF" 
         android:textColorHighlight="#000000" 
         android:textSize="24sp" 
         android:layout_centerInParent="true" 
         android:layout_centerVertical="true" /> 

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_saveEffect" 
         android:onClick="btn_saveEffect_Click" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:padding="10dip" 
         android:layout_alignParentRight="true" 
         android:src="@drawable/tick" 
         android:scaleType="centerInside" 
         android:adjustViewBounds="true" /> 
       </RelativeLayout> 

     </android.support.v7.widget.Toolbar> 

     <android.support.v7.widget.Toolbar 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar_effects_footer" 
      android:layout_width="fill_parent" 
      android:layout_height="128dp" 
      android:layout_alignParentBottom="true" 
      android:background="#1e252d" 
      android:minHeight="?attr/actionBarSize" > 

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

       <ImageView 
        android:id="@+id/imgOverview" 
        android:layout_width="250dp" 
        android:layout_height="match_parent" 
        android:background="@android:color/white" 
        android:visibility="gone" /> 

       <SeekBar 
        android:id="@+id/seekBarObjectives" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:progressDrawable="@drawable/red_scrubber_progress" 
        android:thumb="@drawable/red_scrubber_control" 
        android:layout_centerInParent="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft = "550dp" 
        android:visibility="gone" /> 

       <!-- MAIN MENU DOSYALAR ALT MENUSU BAŞLANGIÇ --> 
       <LinearLayout 
        android:id="@+id/lyt_dosyalar_altmenu" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center" 
        android:paddingBottom="@dimen/activity_vertical_margin" 
        android:paddingLeft="@dimen/activity_horizontal_margin" 
        android:paddingRight="@dimen/activity_horizontal_margin" 
        android:paddingTop="@dimen/activity_vertical_margin" 
        android:orientation="horizontal"> 

        <ImageButton 
          android:background="?attr/selectableItemBackground" 
          android:id="@+id/btn_fileOpen" 
          android:onClick="btn_fileOpen_Click" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingLeft="30dp" 
          android:paddingRight="30dp" 
          android:src="@drawable/file_open" /> 

        <ImageButton 
          android:background="?attr/selectableItemBackground" 
          android:id="@+id/btn_saveScreenshot" 
          android:onClick="btn_saveScreenshot_Click" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingLeft="30dp" 
          android:paddingRight="30dp" 
          android:src="@drawable/save_screenshot" /> 

        <ImageButton 
          android:background="?attr/selectableItemBackground" 
          android:id="@+id/btn_settings" 
          android:onClick="btn_settings_Click" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingLeft="30dp" 
          android:paddingRight="30dp" 
          android:src="@drawable/settings" /> 

       </LinearLayout> 
       <!-- MAIN MENU DOSYALAR ALT MENUSU BİTİŞ --> 

       <!-- MAIN MENU OBJEKTİFLER ALT MENUSU BAŞLANGIÇ --> 
       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="25dp" 
        android:orientation="horizontal" 
        android:layout_marginTop="90dp" 
        android:layout_marginLeft = "290dp" 
        android:id="@+id/lyt_objektifler_altmenu" 
        android:visibility="gone"> 

        <TextView 
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:text="Fit" 
          android:textSize="16dp" 
          android:textColor="#ffffff"/> 

        <TextView 
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:text="1.25X" 
          android:textSize="16dp" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 

        <TextView 
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:textSize="16dp" 
          android:text="10X" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 
        <TextView 
          android:layout_height="wrap_content"  
          android:textSize="16dp" 
          android:layout_width="wrap_content" 
          android:text="20X" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 

        <TextView 
          android:layout_height="wrap_content" 
          android:textSize="16dp" 
          android:layout_width="wrap_content" 
          android:text="40X" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 

        <TextView 
          android:textSize="16dp" 
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:text="63X" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 

        <TextView 
          android:textSize="16dp" 
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:text="100X" 
          android:textColor="#ffffff" 
          android:layout_marginLeft="80dp"/> 

       </LinearLayout> 
       <!-- MAIN MENU OBJEKTİFLER ALT MENUSU BİTİŞ --> 

       <!-- MAIN MENU EFEKTLER ALT MENUSU BAŞLANGIÇ --> 
       <LinearLayout 
        android:id="@+id/lyt_efektler_altmenu" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center" 
        android:paddingBottom="@dimen/activity_vertical_margin" 
        android:paddingLeft="@dimen/activity_horizontal_margin" 
        android:paddingRight="@dimen/activity_horizontal_margin" 
        android:paddingTop="@dimen/activity_vertical_margin" 
        android:orientation="horizontal" 
        android:visibility="gone" > 

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_gamma" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:paddingLeft="30dp" 
         android:paddingRight="30dp" 
         android:src="@drawable/gamma" /> 

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_contrast" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:paddingLeft="30dp" 
         android:paddingRight="30dp" 
         android:src="@drawable/contrast" /> 

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_sharpen" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:paddingLeft="30dp" 
         android:paddingRight="30dp" 
         android:src="@drawable/sharpen" /> 

        <ImageButton 
         android:background="?attr/selectableItemBackground" 
         android:id="@+id/btn_opacity" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:paddingLeft="30dp" 
         android:paddingRight="30dp" 
         android:src="@drawable/opacity" /> 

        <ImageButton 
           android:background="?attr/selectableItemBackground" 
           android:id="@+id/btn_default" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:paddingLeft="30dp" 
           android:paddingRight="30dp" 
           android:src="@drawable/default_effect" /> 
       </LinearLayout> 
       <!-- MAIN MENU EFEKTLER ALT MENUSU BİTİŞ --> 


       <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/txtImgName" 
         android:text="Dosya Seçilmedi." 
         android:textColor="#FFFFFF" 
         android:textColorHighlight="#000000" 
         android:textSize="10sp" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentBottom="true" /> 

      </RelativeLayout>    

    </android.support.v7.widget.Toolbar> 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar_seekbarEffect" 
     android:layout_width="fill_parent" 
     android:layout_height="128dp" 
     android:layout_alignParentBottom="true" 
     android:background="#1e252d" 
     android:visibility="gone" 
     android:minHeight="?attr/actionBarSize" > 

      <RelativeLayout 
       android:id="@+id/lytFooter_seekbar" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:paddingLeft="100dp" 
       android:paddingRight="100dp" 
       android:gravity="center" > 

        <SeekBar 
         android:id="@+id/seekBar" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:progressDrawable="@drawable/red_scrubber_progress" 
         android:thumb="@drawable/red_scrubber_control" /> 

        <TextView 
         android:id="@+id/seekBar_Value" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:textColor="#FFFFFF" 
         android:textColorHighlight="#000000" 
         android:textSize="14sp" /> 

      </RelativeLayout> 

    </android.support.v7.widget.Toolbar> 

    <ProgressBar 
     android:id="@+id/loadingBar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:indeterminate="true" 
     android:indeterminateDrawable="@drawable/custom_loading" 
     android:visibility="gone" /> 

    <android.opengl.GLSurfaceView 
    android:id="@+id/effectsview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.05" /> 
</RelativeLayout> 
+2

那不是我。但也許它有太多的代碼和......除了標題之外沒有任何描述。 – Mauker

+0

@Mauker無需描述,當我通過findviewbyid設置glsurfaceview時,我得到了nullpointerexception – Dauezevy

+0

您是否在'setContentView'方法中使用了正確的引用? – Mauker

回答

1

首先,不要使用fill_parent,它已被棄用。改爲使用match_parent

現在您的代碼,試圖改變這一點:

GPUImage mGPUImage = new GPUImage(this); 
mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.effectsview)); 

要這樣:

GPUImage mGPUImage = new GPUImage(this); 
GLSurfaceView glView = (GLSurfaceView) this.findViewById(R.id.effectsview); 
glView.setRenderer(this); // "this" refers to a renderer. I'm assuming your class implements it. 
mGPUImage.setGLSurfaceView(glView); 
+0

也不需要setRenderer,感謝您的幫助。 :))) – Dauezevy

+0

你非常歡迎! – Mauker