我正在构建一个android应用程序,并想在我的Android应用程序中实现‘屏幕截图’功能和‘共享’选项在点击共享按钮。
但在运行时,它会抛出以下错误,并且单击按钮时没有任何反应。请帮帮我!
尝试加载暂存凭据时出错: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法的java.lang.String
代码如下:
public class ShareActivity extends MainActivity{
Button share;
File imagePath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
share = (Button)findViewById(R.id.share1);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
shareIt();
}
});
}
//Screenshot method to take the screenshot in app
public Bitmap takeScreenshot() {
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
//View rootView = findViewById(android.R.id.content).getRootView();
//rootView.setDrawingCacheEnabled(true);
//return rootView.getDrawingCache();
//View screenView = view.getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
return bitmap;
}
public void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
//ShareIt method to invoke the share the screen shot captured
private void shareIt() {
Uri uri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
Intent intent = this.getIntent();
Bundle bundle= getIntent().getExtras();
sharingIntent = intent.setType("image/*");
//sharingIntent.setType("image/*");
String shareBody = "In Tweecher, My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
堆栈跟踪日志:
09-14 21:25:21.470 2844-2844/com.mycompany.myfirstglapp I/art: Not late-enabling -Xcheck:jni (already on)
09-14 21:25:21.881 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: Telemetry initialize() called...
09-14 21:25:21.883 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: Right before Telemetry set enabled in initialized()
09-14 21:25:21.888 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: setTelemetryEnabled(); this.telemetryEnabled = false; telemetryEnabled = true
09-14 21:25:21.888 2844-2844/com.mycompany.myfirstglapp D/MapboxEventManager: Starting Telemetry Up!
09-14 21:25:21.921 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: Permissions are good, see if GPS is enabled and if not then setup Ambient.
09-14 21:25:21.923 2844-2844/com.mycompany.myfirstglapp E/MapboxEventManager: Error Trying to load Staging Credentials: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
09-14 21:25:21.996 2844-2844/com.mycompany.myfirstglapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-14 21:25:22.263 2844-2844/com.mycompany.myfirstglapp I/art: Thread[1,tid=2844,Native,Thread*=0xb40f4500,peer=0x73828258,"main"] recursive attempt to load library "/data/app/com.mycompany.myfirstglapp-2/lib/x86/libmapbox-gl.so"
09-14 21:25:22.263 2844-2844/com.mycompany.myfirstglapp I/art: Thread[1,tid=2844,Native,Thread*=0xb40f4500,peer=0x73828258,"main"] recursive attempt to load library "/data/app/com.mycompany.myfirstglapp-2/lib/x86/libmapbox-gl.so"
09-14 21:25:22.294 2844-2844/com.mycompany.myfirstglapp E/OfflineManager: Failed to read the storage key: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String, boolean)' on a null object reference
09-14 21:25:22.359 2844-2844/com.mycompany.myfirstglapp I/com.mapbox.mapboxsdk.maps.MapView: MapView start Telemetry...
09-14 21:25:22.360 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: Telemetry initialize() called...
09-14 21:25:22.360 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: Mapbox Telemetry has already been initialized.
[ 09-14 21:25:22.361 2844: 2844 D/ ]
HostConnection::get() New Host Connection established 0xaaabfdc0, tid 2844
09-14 21:25:22.387 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: EGL Vendor: Android
09-14 21:25:22.387 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: EGL Version: 1.4 Android META-EGL
09-14 21:25:22.387 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: EGL Client APIs: OpenGL_ES
09-14 21:25:22.387 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: EGL Client Extensions: EGL_KHR_get_all_proc_addresses EGL_ANDROID_presentation_time EGL_KHR_swap_buffers_with_damage EGL_KHR_image_base EGL_KHR_gl_texture_2D_image EGL_KHR_fence_sync EGL_ANDROID_image_native_buffer
09-14 21:25:22.387 2844-2844/com.mycompany.myfirstglapp W/mbgl: {ny.myfirstglapp}[Android]: In emulator! Enabling hacks :-(
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: Found 2 configs
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: Config 0:
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Caveat: 12344
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Conformant: 5
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Color: 32
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Red: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Green: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Blue: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Alpha: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Alpha mask: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Depth: 24
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Stencil: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Sample buffers: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Samples: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: Config 1:
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Caveat: 12344
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Conformant: 5
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Color: 32
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Red: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Green: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Blue: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Alpha: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Alpha mask: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Depth: 24
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Stencil: 8
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Sample buffers: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: ...Samples: 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: Chosen config is 0
09-14 21:25:22.388 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: Chosen window format is 1
09-14 21:25:22.401 2844-2844/com.mycompany.myfirstglapp I/MapboxEventManager: flushEventsQueueImmediately() called...
09-14 21:25:22.403 2844-2844/com.mycompany.myfirstglapp D/MapboxEventManager: turnstile event pushed.
09-14 21:25:22.419 2844-2844/com.mycompany.myfirstglapp D/Network: Network
09-14 21:25:22.422 2844-2844/com.mycompany.myfirstglapp D/GPS Enabled: GPS Enabled
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.content.ContextWrapper.checkPermission(ContextWrapper.java:637)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:387)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at com.mycompany.myfirstglapp.GPSTracker.getLocation(GPSTracker.java:133)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at com.mycompany.myfirstglapp.GPSTracker.<init>(GPSTracker.java:62)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at com.mycompany.myfirstglapp.MainActivity.onCreate(MainActivity.java:102)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.Activity.performCreate(Activity.java:6237)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
09-14 21:25:22.434 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 21:25:22.435 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.os.Looper.loop(Looper.java:148)
09-14 21:25:22.435 2844-2844/com.mycompany.myfirstglapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
09-14 21:25:22.435 2844-2844/com.mycompany.myfirstglapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-14 21:25:22.435 2844-2844/com.mycompany.myfirstglapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-14 21:25:22.435 2844-2844/com.mycompany.myfirstglapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-14 21:25:22.446 2844-2872/com.mycompany.myfirstglapp D/GzipRequestInterceptor: Compressing
09-14 21:25:22.450 2844-2877/com.mycompany.myfirstglapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-14 21:25:22.456 2844-2844/com.mycompany.myfirstglapp I/TelemetryService: onCreate() called
09-14 21:25:22.456 2844-2844/com.mycompany.myfirstglapp I/TelemetryService: onStartCommand() called
09-14 21:25:22.582 2844-2877/com.mycompany.myfirstglapp I/OpenGLRenderer: Initialized EGL, version 1.4
[ 09-14 21:25:22.583 2844: 2877 D/ ]
HostConnection::get() New Host Connection established 0xaa142310, tid 2877
09-14 21:25:22.699 2844-2877/com.mycompany.myfirstglapp W/EGL_emulation: eglSurfaceAttrib not implemented
09-14 21:25:22.699 2844-2877/com.mycompany.myfirstglapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0453360, error=EGL_SUCCESS
[ 09-14 21:25:22.705 2878: 2903 D/ ]
HostConnection::get() New Host Connection established 0xae4d28d0, tid 2903
09-14 21:25:22.829 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: GL Vendor: Google (Intel)
09-14 21:25:22.829 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: GL Renderer: Android Emulator OpenGL ES Translator (Intel(R) HD Graphics 2000)
09-14 21:25:22.830 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: GL Version: OpenGL ES 2.0 (3.1.0 - Build 9.17.10.4229)
09-14 21:25:22.830 2844-2844/com.mycompany.myfirstglapp I/mbgl: {ny.myfirstglapp}[OpenGL]: GL Extensions: GL_EXT_debug_marker GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint GL_OES_texture_float GL_OES_texture_float_linear GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_packed_depth_stencil GL_OES_vertex_half_float GL_OES_texture_npot GL_OES_rgb8_rgba8 ANDROID_EMU_CHECKSUM_HELPER_v1
09-14 21:25:22.894 2844-2844/com.mycompany.myfirstglapp I/MapAsync: is called
09-14 21:25:23.208 2844-2854/com.mycompany.myfirstglapp I/art: Background partial concurrent mark sweep GC freed 7816(378KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 5MB/9MB, paused 9.556ms total 19.844ms
09-14 21:25:23.290 2844-2854/com.mycompany.myfirstglapp W/art: Suspending all threads took: 81.700ms
09-14 21:25:23.315 2844-2844/com.mycompany.myfirstglapp W/mbgl: {ny.myfirstglapp}[OpenGL]: Not using Vertex Array Objects
09-14 21:25:23.609 2844-2844/com.mycompany.myfirstglapp I/Choreographer: Skipped 45 frames! The application may be doing too much work on its main thread.
09-14 21:25:23.809 2844-2923/com.mycompany.myfirstglapp D/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request with response code = 304: Not Modified
09-14 21:25:23.811 2844-2872/com.mycompany.myfirstglapp D/MapboxEventManager: response code = 204 for events 2
09-14 21:25:24.072 2844-2935/com.mycompany.myfirstglapp V/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request was successful (code = 200).
09-14 21:25:24.200 2844-2934/com.mycompany.myfirstglapp V/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request was successful (code = 200).
09-14 21:25:24.237 2844-2923/com.mycompany.myfirstglapp V/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request was successful (code = 200).
09-14 21:25:24.447 2844-2911/com.mycompany.myfirstglapp D/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request with response code = 304: Not Modified
09-14 21:25:24.502 2844-2935/com.mycompany.myfirstglapp V/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request was successful (code = 200).
09-14 21:25:24.525 2844-2892/com.mycompany.myfirstglapp D/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request with response code = 304: Not Modified
09-14 21:25:24.541 2844-2913/com.mycompany.myfirstglapp D/com.mapbox.mapboxsdk.http.HTTPRequest: [HTTP] Request with response code = 304: Not Modified
09-14 21:25:31.922 2844-3063/com.mycompany.myfirstglapp D/MapboxEventManager: No events in the queue to send so returning.
发布于 2016-09-15 07:56:41
您从日志猫发布的唯一堆栈跟踪是针对错误的
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
当你往下看几行时,你会发现
at com.mycompany.myfirstglapp.GPSTracker.getLocation(GPSTracker.java:133)
这意味着错误是由于GPSTracker.java中的第133行上的某些内容而发生的。这位于您的GPSTracker
类的getLocation()
方法中。您需要查看此行代码,并使用调试器来确定该行代码中的哪个变量为null
。由于堆栈跟踪显示了调用堆栈中的其他方法,因此问题很可能出在传递给getLocation()
方法的一个参数上。
https://stackoverflow.com/questions/39480154
复制相似问题