gpt4 book ai didi

Android 应用程序崩溃,无法理解 logcat 以查找原因

转载 作者:行者123 更新时间:2023-11-29 14:22:03 25 4
gpt4 key购买 nike

我在主要 Activity 中有这段代码:

package flash.light.pro;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.graphics.Color;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.view.View.OnLongClickListener;


public class MainActivity extends Activity {
private boolean isBlack = false;
private boolean isLedOn = false;
private boolean supportsCamera = true;
private Camera camera;
final Parameters p = camera.getParameters();
protected void onStop() {
super.onStop();

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

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
final Button screen = (Button) findViewById (R.id.screen);
Context context = this;
PackageManager pm = context.getPackageManager();

isBlack = false;
setBrightness(255);

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
supportsCamera = false;
}


try{
camera = Camera.open();
} catch (Exception e){
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}

screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (isBlack){
screen.setBackgroundColor(Color.WHITE);
isBlack = false;
setBrightness(255);
}
else{
screen.setBackgroundColor(Color.BLACK);
isBlack = true;
setBrightness(0);
}
}
});

if(supportsCamera){
screen.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
if(isLedOn){
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
return true;
}
else{
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
return true;
}
}
});
}
}

private void setBrightness(int brightness) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = brightness / 100.0f;
getWindow().setAttributes(layoutParams);
}
}

和这个 androidManifest.xml 代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="flash.light.pro"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="string/permlab_flashlight"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

这是 LogCat 日志:

11-14 20:05:06.772: D/AndroidRuntime(380): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-14 20:05:06.772: D/AndroidRuntime(380): CheckJNI is ON
11-14 20:05:07.371: D/AndroidRuntime(380): Calling main entry com.android.commands.pm.Pm
11-14 20:05:07.392: D/AndroidRuntime(380): Shutting down VM
11-14 20:05:07.400: I/AndroidRuntime(380): NOTE: attach of thread 'Binder Thread #3' failed
11-14 20:05:07.411: D/dalvikvm(380): GC_CONCURRENT freed 101K, 71% free 297K/1024K, external 0K/0K, paused 1ms+1ms
11-14 20:05:07.411: D/dalvikvm(380): Debugger has detached; object registry had 1 entries
11-14 20:05:07.830: D/AndroidRuntime(390): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-14 20:05:07.830: D/AndroidRuntime(390): CheckJNI is ON
11-14 20:05:08.390: D/AndroidRuntime(390): Calling main entry com.android.commands.am.Am
11-14 20:05:08.420: I/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]

flg=0x10000000 cmp=flash.light.pro/.MainActivity } from pid 390
11-14 20:05:08.450: I/ActivityManager(61): Start proc flash.light.pro for activity flash.light.pro/.MainActivity: pid=398 uid=10040 gids={1006}
11-14 20:05:08.480: D/AndroidRuntime(390): Shutting down VM
11-14 20:05:08.490: D/dalvikvm(390): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 1ms+3ms
11-14 20:05:08.500: D/dalvikvm(390): Debugger has detached; object registry had 1 entries
11-14 20:05:09.270: D/AndroidRuntime(398): Shutting down VM
11-14 20:05:09.270: W/dalvikvm(398): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 20:05:09.330: E/AndroidRuntime(398): FATAL EXCEPTION: main
11-14 20:05:09.330: E/AndroidRuntime(398): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

{flash.light.pro/flash.light.pro.MainActivity}: java.lang.NullPointerException
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.os.Looper.loop(Looper.java:123)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 20:05:09.330: E/AndroidRuntime(398): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398): at java.lang.reflect.Method.invoke(Method.java:507)
11-14 20:05:09.330: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 20:05:09.330: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 20:05:09.330: E/AndroidRuntime(398): at dalvik.system.NativeStart.main(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
11-14 20:05:09.330: E/AndroidRuntime(398): at flash.light.pro.MainActivity.<init>(MainActivity.java:22)
11-14 20:05:09.330: E/AndroidRuntime(398): at java.lang.Class.newInstanceImpl(Native Method)
11-14 20:05:09.330: E/AndroidRuntime(398): at java.lang.Class.newInstance(Class.java:1409)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-14 20:05:09.330: E/AndroidRuntime(398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
11-14 20:05:09.330: E/AndroidRuntime(398): ... 11 more
11-14 20:05:09.410: W/ActivityManager(61): Force finishing activity flash.light.pro/.MainActivity
11-14 20:05:09.940: W/ActivityManager(61): Activity pause timeout for HistoryRecord{4060c450 flash.light.pro/.MainActivity}
11-14 20:05:20.739: W/ActivityManager(61): Activity destroy timeout for HistoryRecord{4060c450 flash.light.pro/.MainActivity}
11-14 20:06:00.031: I/dalvikvm(61): Jit: resizing JitTable from 1024 to 2048
11-14 20:06:19.377: D/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol
11-14 20:08:35.071: I/Process(398): Sending signal. PID: 398 SIG: 9
11-14 20:08:35.081: I/ActivityManager(61): Process flash.light.pro (pid 398) has died.
11-14 20:08:35.111: W/InputManagerService(61): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub

$Proxy@4051a740

应用程序在启动时崩溃,没有错误,日志对我来说也不清楚。我能做些什么来解决它。通过为什么应用程序应该是某种手电筒。感谢您的帮助。

最佳答案

private Camera camera;
final Parameters p = camera.getParameters();

相机尚未初始化

通过查看 logcat 输出指示的第 22 行,我能够发现这一点。 不要被 logcat 中的信息量淹没,通常找到您的包名称的第一个实例将指示问题所在/问题所在。

编辑:作为旁注,您的包名称应该以相反的域顺序排列。因此,例如,如果您的应用名为 flashlightpro 而您的域名为 example.com,则您的包名称应为 com.example.flashlightpro。这不是必需的,但它是一个标准约定。

编辑 2:将相机的声明更改为:Camera camera = Camera.open();

编辑 3:Android 文档涉及拍照,但第 1、2、3、10 步对您的情况很重要:

Obtain an instance of Camera from open(int).
Get existing (default) settings with getParameters().
If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
...
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open()it in onResume()).

http://developer.android.com/reference/android/hardware/Camera.html

关于Android 应用程序崩溃,无法理解 logcat 以查找原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13386567/

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