更新
它现在没有出现错误,但是我登录后,拥有应用程序的Facebook好友列表也没有出现在主视图中,如果有人可以提供帮助的话,那太棒了。 我已将项目上传至HERE,以便任何人查看,并让我知道我哪里出错了 如果你能让我知道哪个文件和哪个部分有问题,以及如何修复它,那就太棒了!
我正在尝试获取一个朋友列表,在他们登录时显示其他人在某人的Facebook好友列表中有该应用,并且我一直收到错误" java.lang.NullPointerException:尝试调用虚方法' int java.lang.String.length()'在null对象引用"
发生错误是因为savedInstanceState始终为null我相信,根据调试器...任何想法?完整代码如下:
package com.phinder.phinder;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public com.phinder.phinder.utils.Constants constants;
ImageView imageView_picture_profile;
TextView textview_nameprofile, textview_emailprofile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constants = new com.phinder.phinder.utils.Constants();
imageView_picture_profile = (ImageView) findViewById(R.id.imageView_picture_profile);
textview_emailprofile = (TextView) findViewById(R.id.textview_emailprofile);
textview_nameprofile = (TextView) findViewById(R.id.textview_nameprofile);
textview_emailprofile.setText(constants.loginSharedPreferences.getString(constants.LoginEmail, ""));
textview_nameprofile.setText(constants.loginSharedPreferences.getString(constants.LoginName, ""));
try {
Glide.with(getApplicationContext())
.load("http://" + constants.loginSharedPreferences.getString(constants.LoginImage, ""))
.placeholder(R.mipmap.ic_launcher)
.thumbnail(.1F)
.error(R.mipmap.ic_launcher)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView_picture_profile);
} catch (Exception e) {
e.getStackTrace();
Log.d("msg", "Photo " + e.getMessage());
}
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String jsondata = intent.getStringExtra("jsondata");
JSONArray friendslist;
ArrayList<String> friends = new ArrayList<>();
try {
friendslist = new JSONArray(jsondata);
for (int l = 0; l < friendslist.length(); l++) {
friends.add(friendslist.getJSONObject(l).getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter adapter = new ArrayAdapter<>(MainActivity.this, R.layout.activity_main, friends);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
我对Android编码很陌生,很抱歉,如果这看起来很明显!
我已经在下面添加了清单,以防需要:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phinder.phinder">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity android:name=".MainActivity"></activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
</application>
</manifest>
这也是一个错误日志(删除了从Facebook检索时放在那里的一些个人信息):
12/10 23:40:03: Launching app
$ adb shell am start -n "com.phinder.phinder/com.phinder.phinder.LoginActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.phinder.phinder.test | com.phinder.phinder
Waiting for application to come online: com.phinder.phinder.test | com.phinder.phinder
Waiting for application to come online: com.phinder.phinder.test | com.phinder.phinder
Connecting to com.phinder.phinder
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
E/Zygote: v2
I/SELinux: Function: selinux_compare_spd_ram, index[1], SPD-policy is existed. and_ver=SEPF_SECMOBILE_6.0.1 ver=11
I/libpersona: KNOX_SDCARD checking this for 10412
I/libpersona: KNOX_SDCARD not a persona
W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [1], priority version is VE=SEPF_SECMOBILE_6.0.1_0011
E/Zygote: accessInfo : 0
W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.phinder.phinder
I/art: Late-enabling -Xcheck:jni
D/TimaKeyStoreProvider: TimaSignature is unavailable
D/ActivityThread: Added TimaKeyStore provider
I/System.out: Sending WAIT chunk
W/ActivityThread: Application com.phinder.phinder is waiting for the debugger on port 8100...
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1439)
W/System: ClassLoader referenced unknown path: /data/app/com.phinder.phinder-1/lib/arm
I/InstantRun: Instant Run Runtime started. Android package is com.phinder.phinder, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.phinder.phinder-1/lib/arm
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
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.phinder.phinder/files/AppEventsLogger.persistedevents
D/Package Name=: com.phinder.phinder
D/Key Hash=: zs7kzMXx78uDoJ9QjcsCuL2xPY0=
W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.phinder.phinder/files/AppEventsLogger.persistedsessioninfo
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{802af0f I.E...... R.....ID 0,0-0,0}
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
D/libEGL: eglInitialize EGLDisplay = 0x9e67f7c4
I/OpenGLRenderer: Initialized EGL, version 1.4
D/mali_winsys: new_window_surface returns 0x3000, [2560x1440]-format:1
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=2
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@266ed2f time:66569701
W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.phinder.phinder/files/AppEventsLogger.persistedevents
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/Timeline: Timeline: Activity_launch_request id:com.phinder.phinder time:66595549
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{cb1dd37 I.E...... R.....I. 0,0-0,0}
D/mali_winsys: new_window_surface returns 0x3000, [2560x1440]-format:1
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=2
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@b071cae time:66595716
V/ActivityThread: updateVisibility : ActivityRecord{1393c2f token=android.os.BinderProxy@266ed2f {com.phinder.phinder/com.phinder.phinder.LoginActivity}} show : true
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{9b1361b V.E...... R.....I. 0,0-0,0}
D/msg: Facebook
D/msg: fbid : 10154826532222425
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/mali_winsys: new_window_surface returns 0x3000, [1952x720]-format:1
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@266ed2f time:66596446
D/ViewRootImpl: #3 mView = null
I/Timeline: Timeline: Activity_launch_request id:com.phinder.phinder time:66596581
D/AbsListView: Get MotionRecognitionManager
E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@4d4175c
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@4394d48
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@4394d48
D/AbsListView: Get MotionRecognitionManager
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{f7ccc01 I.E...... R.....ID 0,0-0,0}
D/mali_winsys: new_window_surface returns 0x3000, [2560x1440]-format:1
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=2
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5176ada time:66596826
V/ActivityThread: updateVisibility : ActivityRecord{1393c2f token=android.os.BinderProxy@266ed2f {com.phinder.phinder/com.phinder.phinder.LoginActivity}} show : false
D/msg: Fb data1 : {Response: responseCode: 200, graphObject: {"id":"10154826532222425","name":"Dean Mokhtar","location":{"id":"109563165735553","name":"October 6, Al Jizah, Egypt"},"email":"mokdeabar@hotmail.com","picture":{"data":{"is_silhouette":false,"url":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/s200x200\/1471998_10152127224047425_6664922_n.jpg?oh=b5df53e5296ebea31a34126407b64933&oe=58EA2DCC"}}}, error: null}
D/msg: Fb data : {"id":"10154826532222425","name":"DM","location":{"id":"","name":""},"email":"@","picture":{"data":{"is_silhouette":false,"url":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/s200x200\/1471998_10152127224047425_6664922_n.jpg?oh=b5df53e5296ebea31a34126407b64933&oe=58EA2DCC"}}}
D/email:
I/WebViewFactory: Loading com.google.android.webview version 55.0.2883.91 (code 288309100)
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
I/cr_LibraryLoader: Time to load native libraries: 1 ms (timestamps 6941-6942)
I/cr_LibraryLoader: Expected native library version number "55.0.2883.91", actual native library version number "55.0.2883.91"
I/Timeline: Timeline: Activity_launch_request id:com.phinder.phinder time:66596962
D/ViewRootImpl: #3 mView = null
D/ViewRootImpl: #3 mView = null
D/AbsListView: Get MotionRecognitionManager
E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@59d2e50
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@9e94849
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@9e94849
D/AbsListView: Get MotionRecognitionManager
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phinder.phinder, PID: 9793
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phinder.phinder/com.phinder.phinder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.phinder.phinder.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.phinder.phinder/files/AppEventsLogger.persistedevents
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/Process: Sending signal. PID: 9793 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
答案 0 :(得分:0)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.phinder.phinder.MainActivity.onCreate(MainActivity.java:61)
有问题的错误发生在JsonArray
的构造函数中。
这表明问题在于行friendslist = new JSONArray(jsondata)
。
由于有问题的错误是NullPointerException
,因此intent.getStringExtra("jsondata")
可能会返回null,这是因为没有值。
要修复错误,请确保启动活动的任何人都包含Intent中的其他数据,或检查数据是否存在并以您自己的方式处理数据的缺失。