您好我的程序在纵向模式和手机上工作正常,但在平板电脑中它不会工作并强制关闭错误是这个
05-31 07:07:10.888: E/AndroidRuntime(1012): FATAL EXCEPTION: main
05-31 07:07:10.888: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soheil.prolight/com.soheil.prolight.MainActivity}: java.lang.NullPointerException: println needs a message
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.os.Looper.loop(Looper.java:137)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-31 07:07:10.888: E/AndroidRuntime(1012): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 07:07:10.888: E/AndroidRuntime(1012): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 07:07:10.888: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-31 07:07:10.888: E/AndroidRuntime(1012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-31 07:07:10.888: E/AndroidRuntime(1012): at dalvik.system.NativeStart.main(Native Method)
05-31 07:07:10.888: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException: println needs a message
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.util.Log.println_native(Native Method)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.util.Log.e(Log.java:231)
05-31 07:07:10.888: E/AndroidRuntime(1012): at com.soheil.prolight.MainActivity.getCamera(MainActivity.java:184)
05-31 07:07:10.888: E/AndroidRuntime(1012): at com.soheil.prolight.MainActivity.onCreate(MainActivity.java:146)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.Activity.performCreate(Activity.java:4465)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-31 07:07:10.888: E/AndroidRuntime(1012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
这是我的代码,我认为我不会在手机上出现任何错误:
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>
我想知道错误来自哪里?
答案 0 :(得分:0)
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
异常说log的第二个参数需要是一个有效的String。它不能为空。 e.getMessage()
可以返回null。
Log.e("Camera Error. Failed to Open. Error: ", (e.getMessage() != null) ? e.getMessage() : e);
答案 1 :(得分:0)
// Get the camera
private void getCamera() {
if (camera == null) {
camera = Camera.open();
if (camera != null) {
params = camera.getParameters();
}
}
}
NullPointerException
吗?没有。答案 2 :(得分:0)
它说有与打印功能有关的错误。它发生在活动开始之前,这意味着onResume(),onStart()可能存在问题...... 我想你应该考虑这些:
在turnOnFlash之前调用getCamera
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
turnOnFlash();
}