gpt4 book ai didi

java.lang.NullPointerException 致命异常 : IntentService[GCMIntentService-876799952849-1]

转载 作者:行者123 更新时间:2023-12-01 12:14:39 27 4
gpt4 key购买 nike

请在将其标记为重复之前完整阅读我的问题,我已经彻底搜索了这种情况。我目前正在开发一个通过网站远程控制您的 Android 设备的 Android 应用程序。目前,当我我正在尝试发送命令来启动录音机。我创建了一个名为audiorecorder.java的类,当消息“startrecorder”从服务器端到达时创建其对象。该消息实际上在 gcmintent service.java 中进行比较。我是粘贴代码 fragment 以及 logcat。请帮助我,因为这个错误不仅出现在录音机功能的 acse 中,而且在其他情况下也出现,例如拍摄 scrrenshot 或从相机拍照。提前致谢。

录音机.java

package com.androidhive.pushnotifications;

import java.io.File;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

import android.app.Activity;

public class AudioRecorder extends Activity {
final MediaRecorder recorder = new MediaRecorder();
public String path;

public void onCreate(String path) {
this.path = sanitizePath(path);
}

private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ path;
}

public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}

// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}

public void stop() throws IOException {
recorder.stop();
recorder.release();
}

public void playrecording(String path) throws IOException {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
mp.setVolume(10, 10);
}

}





Gcmintentservice.java --->
else if(message.equalsIgnoreCase ("startrecorder")){

AudioRecorder a = new AudioRecorder();
try {
a.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}




Logcat:

11-23 11:36:11.338: D/dalvikvm(2313): GC_EXTERNAL_ALLOC freed 51K, 51% free 2687K/5379K, external 0K/0K, paused 362ms
11-23 11:36:11.438: D/dalvikvm(2313): GC_EXTERNAL_ALLOC freed 1K, 51% free 2688K/5379K, external 0K/512K, paused 88ms
11-23 11:36:18.685: V/GCMBroadcastReceiver(2313): onReceive: com.google.android.c2dm.intent.RECEIVE
11-23 11:36:18.695: V/GCMBroadcastReceiver(2313): GCM IntentService class: com.androidhive.pushnotifications.GCMIntentService
11-23 11:36:18.695: V/GCMBaseIntentService(2313): Acquiring wakelock
11-23 11:36:18.735: V/GCMBaseIntentService(2313): Intent service name: GCMIntentService-876799952849-1
11-23 11:36:18.755: I/GCMIntentService(2313): Received message
11-23 11:36:18.775: V/GCMBaseIntentService(2313): Releasing wakelock
11-23 11:36:18.775: W/dalvikvm(2313): threadid=10: thread exiting with uncaught exception (group=0x40018560)
11-23 11:36:18.785: E/AndroidRuntime(2313): FATAL EXCEPTION: IntentService[GCMIntentService-876799952849-1]
11-23 11:36:18.785: E/AndroidRuntime(2313): java.lang.NullPointerException
11-23 11:36:18.785: E/AndroidRuntime(2313): at java.io.File.fixSlashes(File.java:205)
11-23 11:36:18.785: E/AndroidRuntime(2313): at java.io.File.init(File.java:189)
11-23 11:36:18.785: E/AndroidRuntime(2313): at java.io.File.<init>(File.java:139)
11-23 11:36:18.785: E/AndroidRuntime(2313): at com.androidhive.pushnotifications.AudioRecorder.start(AudioRecorder.java:39)
11-23 11:36:18.785: E/AndroidRuntime(2313): at com.androidhive.pushnotifications.GCMIntentService.onMessage(GCMIntentService.java:128)
11-23 11:36:18.785: E/AndroidRuntime(2313): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
11-23 11:36:18.785: E/AndroidRuntime(2313): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
11-23 11:36:18.785: E/AndroidRuntime(2313): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 11:36:18.785: E/AndroidRuntime(2313): at android.os.Looper.loop(Looper.java:130)
11-23 11:36:18.785: E/AndroidRuntime(2313): at android.os.HandlerThread.run(HandlerThread.java:60)

最佳答案

这看起来不对:

public String path;

public void onCreate(String path) {
this.path = sanitizePath(path);
}

orCreate 没有 noCreate 的签名Activity 的方法,因此启动 Activity 时不会执行该方法。因此,path 保持为空,导致 File directory = new File(path).getParentFile(); 处的 NullPointerException

如果您希望将 path 变量传递给您的 Activity,您应该将其放入启动 Activity 的 Intent 中。

关于java.lang.NullPointerException 致命异常 : IntentService[GCMIntentService-876799952849-1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087242/

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