gpt4 book ai didi

java - 添加 Google Ads 后应用程序意外关闭

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:20 25 4
gpt4 key购买 nike

我创建了一个手电筒应用程序,它在没有添加任何广告单元的情况下运行完美。但是在代码中添加广告单元后,当我下次运行时,应用程序也会在我的模拟器和 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>

对于上述 Activity ,我在我的 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>

附言代码在编译时没有显示错误。我也检查了 logCat,它在主要 Activity 启动时显示了一些 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

最佳答案

我想我明白@Tyler 提到的这段代码中的实际问题是什么e.getMessage() 有可能为空但进一步他给出的例子有点错误。我曾经声明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。

关于java - 添加 Google Ads 后应用程序意外关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com