Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 仅在tablet上出现空指针异常_Android_Nullpointerexception - Fatal编程技术网

Android 仅在tablet上出现空指针异常

Android 仅在tablet上出现空指针异常,android,nullpointerexception,Android,Nullpointerexception,嗨,我的程序在人像模式和手机上运行良好,但在平板电脑上无法运行,错误是 这是我的代码,考虑到我在手机上不接受YANYY错误: public class MainActivity extends Activity { ImageButton btnSwitch; ImageButton morebtn; ToggleButton tb_mute; AudioManager mAudioManager; boolean mute; private Camera camera; private b

嗨,我的程序在人像模式和手机上运行良好,但在平板电脑上无法运行,错误是

这是我的代码,考虑到我在手机上不接受YANYY错误:

public class MainActivity extends Activity {

ImageButton btnSwitch;
ImageButton morebtn;
ToggleButton tb_mute;
AudioManager mAudioManager;
boolean mute;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
float oldBrightNess = 0;
float newBrightNess = 1.0f;
//add
private AdView adView;
public ContextWrapper context;

public Object paramString;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | 
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    //ads
    //papaya

    AppFlood.initialize(this, "XXXXXXXXX", "xxxxxxxxxxxxxxxx", 
            AppFlood.AD_ALL);


 // Look up the AdView as a resource and load a request.
   AdView adView = (AdView)this.findViewById(R.id.adView);
   AdRequest adRequest = new AdRequest();


    LinearLayout layout = (LinearLayout) findViewById(R.id.mainline);


    // Initiate a generic request to load it with an ad
    adView.loadAd(adRequest);



    morebtn = (ImageButton) findViewById(R.id.imageButton1);
    morebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent newActivity = new Intent(MainActivity.this,more.class);   
                startActivity(newActivity); 

        }
    }); 

    // flash switch button
    btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
  //Mute

    tb_mute = (ToggleButton) findViewById(R.id.toggleButton1);

    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    if(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)==0){
        mute = true;
        tb_mute.setChecked(mute);
    }else{
        mute = false;
        tb_mute.setChecked(mute);
    }
    tb_mute.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            if(!mute){
                mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                tb_mute.setChecked(true);
                mute = true;
            }else{
                mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
                tb_mute.setChecked(false);
                mute = false;
            }


        }
    });  

    // First check if device is supporting flashlight or not        
    hasFlash = getApplicationContext().getPackageManager()
          .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

      getCamera();

      btnSwitch.setOnClickListener(new View.OnClickListener() {

          @Override
        public void onClick(View v) {
              if (!hasFlash) {
                  // turn off flash

                  Intent newActivity = new Intent(MainActivity.this,screenlight.class);   
                           startActivity(newActivity);
                           turnOffFlash();



              } else if (hasFlash && isFlashOn) {
                 playSound();
                  toggleButtonImage();
                  // turn off flash
                  turnOffFlash();
              } else {
                 playSound();
                  toggleButtonImage();  
                  // turn on flash
                  turnOnFlash();
              } 
          }
      });
       return;
  }

// Get the camera
private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }
}

 // Turning On flash
private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound(); 
        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        // changing button/switch image
        toggleButtonImage();
    }

}

// Turning Off flash
private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;

        // changing button/switch image
        toggleButtonImage();
    }
}

 // Playing sound
 // will play button toggle sound on flash on / off
private void playSound(){
    if(isFlashOn){
        mp = MediaPlayer.create(MainActivity.this, R.raw.beep);
    }else{
        mp = MediaPlayer.create(MainActivity.this, R.raw.beep);
    }
    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

            mp.release();
        }
    }); 
    mp.start();
}


/*
 * Toggle switch button images
 * changing image states to on / off
 * */
private void toggleButtonImage(){
    if(isFlashOn){
        btnSwitch.setImageResource(R.drawable.btn_switch_on);
    }else{
        btnSwitch.setImageResource(R.drawable.btn_switch_off);
    }
}

@Override
protected void onPause() {
    if (adView != null) {
          adView.destroy();
        }
 super.onPause();
}
@Override
protected void onDestroy() {
    if (adView != null) {
          adView.destroy();
        }
    AppFlood.destroy();
 super.onDestroy();
}



@Override
protected void onRestart() {
    super.onRestart();
  }

@Override
protected void onResume() {
   super.onResume();

   }

@Override
protected void onStart() {
    super.onStart();
    turnOnFlash();
    // on starting the app get the camera params
    getCamera();
   }

@Override
protected void onStop() {
    super.onStop();

    // on stop release the camera
    if (camera != null) {
        camera.release();
        camera = null;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.mymenu, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.item1){
        Dialog d=new Dialog(MainActivity.this);
        d.setContentView(R.layout.aboutdialog);
        d.setTitle("About");
        d.show();
    }
    return super.onOptionsItemSelected(item);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {

        finish();
    }
    return super.onKeyDown(keyCode, event);
}

}
如果您想要我的布局xml代码,请参见:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:gravity="top"
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/btnSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginTop="25dp"
    android:background="@null"
    android:contentDescription="@null"
    android:src="@drawable/btn_switch_on" />


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:text="@string/Develop"
    android:textColor="#C0C0C0"
    android:textSize="15sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:padding="5dp"
    android:text="@string/soheilen"
    android:textColor="#C0C0C0"
    android:textSize="18sp" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@null"
    android:contentDescription="@null"
    android:minHeight="64dp"
    android:minWidth="64dp"
    android:src="@drawable/more" />

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/mute"
    android:textOff="@string/mute"
    android:textOn="@string/mute" />


    <LinearLayout 

        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:id="@+id/mainline" 
        android:gravity="top"
        >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    ads:adSize="BANNER"
    ads:adUnitId="xxxxxxxx"
    ads:loadAdOnCreate="true"
    android:gravity="top" >
   </com.google.ads.AdView>     

    </LinearLayout>

我想知道错误是从哪里来的

Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
异常表示log的第二个参数必须是有效字符串。它不能为空
e.getMessage()
可以返回null

Log.e("Camera Error. Failed to Open. Error: ", (e.getMessage() != null) ? e.getMessage() : e);
  • 您正在尝试捕获
    NullPointerException
    ?不要
  • 考虑更改函数的名称。获取者返回一些东西

它说有与打印功能相关的错误。它发生在活动开始之前,这意味着onResume()和onStart()中可能存在问题。。。 我认为你应该考虑这些:

  • 注释掉所有日志。。。。这可能会导致打印功能出错
  • 打开闪光灯前呼叫getCamera

    protected void onStart() {
       super.onStart();
    
       // on starting the app get the camera params
       getCamera();
       turnOnFlash();
    }
    

  • 这是试图打印具有空值的内容时发生的nullpointer异常。请发布整个错误日志,以便我们找到发生错误的行。活动中唯一的日志记录命令是Log.e(“摄像头错误。打开失败。错误:”,e.getMessage());可能是这导致了你的error@Mahesh完全错误adedFunny。捕获一个
    RuntimeException
    (您不应该这样做)您被另一个
    RuntimeException
    捕获。是的,您的问题是代码行号146。检查是否为Log.e(“摄像头错误。打开失败。错误:”,e.getMessage());然后通过检查e.getMessage()中的空值来消除此错误。@Mahesh您说话就像一个试图治愈症状的医生。我如何修复此问题,然后删除日志?当我添加代码时,它显示将e强制转换为字符串,当我将e强制转换为logcat上的字符串时,我会收到此错误?05-31 07:19:48.789:E/AndroidRuntime(2437):java.lang.RuntimeException:无法启动活动组件信息{com.soheil.prolight/com.soheil.prolight.MainActivity}:java.lang.ClassCastException:java.lang.NullPointerException无法转换为java.lang.Stringcode:转换后是这样的:Log.E(“摄影机错误。打开失败。错误:”,(字符串)((e.getMessage()!=null)?e.getMessage():e));打印e.printStackTrace()而不是原始答案也不会编译,@Mahesh建议。删除日志会有什么问题,因为我删除了它?只是缺少调试?
    // Get the camera
    private void getCamera() {
        if (camera == null) {
            camera = Camera.open();
            if (camera != null) {
                 params = camera.getParameters();
            }
        }
    }
    
    protected void onStart() {
       super.onStart();
    
       // on starting the app get the camera params
       getCamera();
       turnOnFlash();
    }