gpt4 book ai didi

android - startActivityForResult 中的空指针异常

转载 作者:行者123 更新时间:2023-11-30 04:28:09 26 4
gpt4 key购买 nike

我正在尝试从 HTML5 和 JavaScript 调用相机。我的代码如下。但是我得到空指针异常。

主要 Activity :

 public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webview=(WebView)findViewById(R.id.webkitWebView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
//load html5 page
webview.loadUrl("file:///android_asset/www/html/opencamera.html");

openCamera=new OpenCamera();
webview.addJavascriptInterface(openCamera,"camera");

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK){
System.out.println("Done");
}
}

摄像头通话:

public class OpenCamera extends Activity {  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public void startCameraActivity(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult(intent,0);
}

}

html 页面:

 <!DOCTYPE HTML>  
<html>
<head>
<script type="text/javascript">
function openCamera(){
//alert("cameras");
camera.startCameraActivity();
}
</script>


<title>Welcome</title>
</head>
<body>
Hello World!!!
<br/>
<button type="button" onclick="openCamera()">Capture Image</button>
</body>
</html>

list 中的权限:

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

日志:

11-16 08:56:07.133: ERROR/AndroidRuntime(539): java.lang.NullPointerException  
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.app.Activity.startActivityForResult(Activity.java:2817)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at com.test.camera.OpenCamera.startCameraActivity(OpenCamera.java:17)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.access$3300(WebViewCore.java:52)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1150)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:123)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:621)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at java.lang.Thread.run(Thread.java:1096)

最佳答案

您正在从尚未启动的 OpenCamera Activity 启动 Camera Activity(根据 android 启动 Activity 的方式),因此上下文OpenCamera Activity 为 null,这就是为什么您的 Context 当时为 null 并导致 NullPointerException

解决方法:

将上下文传递给函数 startCameraActivity() 作为

public void startCameraActivity(Context con){  
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
con.startActivityForResult(intent,0);
}

将上下文传递给 WebView

webview.addJavascriptInterface(this,"context");

在你的脚本中

<script type="text/javascript">  
function openCamera(){
camera.startCameraActivity(context);
}
</script>

OpenCamera 不必是 Activity

关于android - startActivityForResult 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146539/

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