在其中添加Google广告后,应用程序意外关闭

时间:2014-03-17 07:04:56

标签: java android ads

我创建了一个手电筒应用程序,它运行完美,无需添加任何广告单元。 但是在代码中添加广告单元后,当我下次运行时,应用程序也会在我的模拟器和Android设备上意外关闭。 这是我上述申请的来源。 activity_main.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:id="@+id/rellayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@layout/radial_background"
tools:context=".MainActivity"
tools:ignore="Overdraw" >
...
...
<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignParentLeft="true"
    ads:adSize="BANNER"
    ads:adUnitId="xxxxxxx"
    ads:loadAdOnCreate="true"
    ads:testDevices="TEST_EMULATOR" >
</com.google.ads.AdView>

对于上述行为,我在AndroidManifest.xml

中进行了更改
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jdev.itorch"
android:versionCode="1"
android:versionName="1.1.2" android:installLocation="auto" xmlns:tools="http://schemas.android.com/tools">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" tools:ignore="OldTargetApi"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity 
        android:name="com.google.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation"
        ></activity>
    <activity
        android:name="com.jdev.itorch.TorchActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

P.S。代码在编译时没有显示错误。 我也检查了logCat,它在主要活动开始时显示一些NullPointerException错误。 我不知道如何处理我的代码,这就是我在这里寻求帮助的原因。

这是全长代码

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;


public class TorchActivity extends Activity {

ImageButton btnSwitch;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
private Parameters params;
MediaPlayer mp;


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_torch);    




    btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);



    hasFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        AlertDialog alert = new AlertDialog.Builder(TorchActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                finish();
            }
        });
        alert.show();
        return;
    }


    getCamera();


    toggleButtonImage();



    btnSwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {

                turnOffFlash();
            } else {

                turnOnFlash();
            }
        }
    });
}



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());
        }
    }
}



private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;


        toggleButtonImage();
    }

}



private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        playSound();

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


        toggleButtonImage();
    }
}



private void playSound(){
    if(isFlashOn){
        mp = MediaPlayer.create(TorchActivity.this, R.raw.light_switch_off);
    }else{
        mp = MediaPlayer.create(TorchActivity.this, R.raw.light_switch_on);
    }
    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.release();
        }
    }); 
    mp.start();
}


private void toggleButtonImage(){
    if(isFlashOn){
        btnSwitch.setImageResource(R.drawable.btn_switch_on);
    }else{
        btnSwitch.setImageResource(R.drawable.btn_switch_off);
    }
}

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

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


    turnOffFlash();
}

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

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


    if(hasFlash)
        turnOnFlash();
}

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


    getCamera();
}

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


    if (camera != null) {
        camera.release();
        camera = null;
    }
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.torch, menu);
    return true;
}

}

更新 这是LogCat数据

03-18 09:11:43.572: D/dalvikvm(1052): GC_FOR_ALLOC freed 80K, 5% free 3068K/3224K, paused 32ms, total 34ms
03-18 09:11:43.582: I/dalvikvm-heap(1052): Grow heap (frag case) to 3.673MB for 635812-byte allocation
03-18 09:11:43.622: D/dalvikvm(1052): GC_FOR_ALLOC freed 4K, 5% free 3684K/3848K, paused 37ms, total 37ms
03-18 09:11:43.792: D/AndroidRuntime(1052): Shutting down VM
03-18 09:11:43.792: W/dalvikvm(1052): threadid=1: thread exiting with uncaught exception (group=0xb3aa7b90)
03-18 09:11:43.832: E/AndroidRuntime(1052): FATAL EXCEPTION: main
03-18 09:11:43.832: E/AndroidRuntime(1052): Process: com.jdev.itorch, PID: 1052
03-18 09:11:43.832: E/AndroidRuntime(1052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jdev.itorch/com.jdev.itorch.TorchActivity}: java.lang.NullPointerException: println needs a message
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.os.Looper.loop(Looper.java:137)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at   android.app.ActivityThread.main(ActivityThread.java:4998)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at java.lang.reflect.Method.invoke(Method.java:515)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at dalvik.system.NativeStart.main(Native Method)
03-18 09:11:43.832: E/AndroidRuntime(1052): Caused by: java.lang.NullPointerException: println needs a message
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.util.Log.println_native(Native Method)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.util.Log.e(Log.java:232)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at com.jdev.itorch.TorchActivity.getCamera(TorchActivity.java:91)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at com.jdev.itorch.TorchActivity.onStart(TorchActivity.java:199)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.Activity.performStart(Activity.java:5253)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2149)
03-18 09:11:43.832: E/AndroidRuntime(1052):     ... 11 more
03-18 09:16:44.422: I/Process(1052): Sending signal. PID: 1052 SIG: 9

2 个答案:

答案 0 :(得分:2)

来自LogCat的这三行看起来很可疑:

03-18 09:11:43.832: E/AndroidRuntime(1052): Caused by: java.lang.NullPointerException: println needs a message
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.util.Log.println_native(Native Method)
03-18 09:11:43.832: E/AndroidRuntime(1052):     at android.util.Log.e(Log.java:232)

这是您唯一的Log.e()电话:

Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());

e.getMessage()可能是null。你应该尝试检查一下。


编辑:这是一个例子:

if(e.getMessage() == null) {
    Log.e("Camera Error.  Failed to Open.",  "Error message is null");
}

else {
    Log.e("Camera Error.  Failed to Open.  Error: ", e.getMessage());
}

答案 1 :(得分:2)

我猜我理解@Tyler There is a possibility that e.getMessage() could be null提到的此代码中的实际问题是什么 但进一步他给出的例子有点错误。 我曾经宣称过 public volatile String errorMessage = "";

我实现如下:

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());
            if(e.getMessage() == null) {
                errorMessage = "Camera Error.  Failed to Open.  Error message is null"; //here//
            }

            else {
                Log.e("Camera Error.  Failed to Open.  Error: ", e.getMessage());
            }
        }
    }
}

感谢您的支持 关于Jai Sharma。