我正在尝试将我的应用程序与社交网络(Facebook)集成,并且需要一些帮助。我想要得到我的饲料。当我运行应用程序时,我收到致命异常错误。
FATAL EXCEPTION: main
Process: com.example.vladzakharo.facebookapp, PID: 2428
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.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.example.vladzakharo.facebookapp.CentralActivity$2.onCompleted(CentralActivity.java:88)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
所以,这是我的班级
public class CentralActivity extends AppCompatActivity {
Button btnPublish;
ListView listView;
public ArrayList<HashMap<String, String>> list;
public String Message;
public String Name;
public String Caption;
public String Description;
public String Picture;
public String Link;
public String Id;
public static final String MESSAGE = "message";
public static final String NAME = "name";
public static final String CAPTION = "caption";
public static final String DESCRIPTION = "description";
public static final String PICTURE = "picture";
public static final String LINK = "link";
public static final String ID = "id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_central);
list = new ArrayList<>();
listView = (ListView) findViewById(R.id.listView);
btnPublish = (Button) findViewById(R.id.btnPublish);
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Publish();
}
});
getFeed();
}
private void Publish(){
Intent goPublish = new Intent(CentralActivity.this, PublishActivity.class);
startActivity(goPublish);
}
protected void getFeed(){
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"me/feed?fields=message,name,caption,description,picture,link",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject json;
try{
json = new JSONObject(response.getRawResponse());
JSONArray jArray = json.getJSONArray("data");
for(int i = 0; i < jArray.length(); i++){
Message = jArray.getJSONObject(i).get("message").toString();
Name = jArray.getJSONObject(i).get("name").toString();
Caption = jArray.getJSONObject(i).get("caption").toString();
Description = jArray.getJSONObject(i).get("description").toString();
Picture = jArray.getJSONObject(i).get("picture").toString();
Link = jArray.getJSONObject(i).get("link").toString();
Id = jArray.getJSONObject(i).get("id").toString();
HashMap<String, String> hm = new HashMap<>();
hm.put(MESSAGE, Message);
hm.put(NAME, Name);
hm.put(CAPTION, Caption);
hm.put(DESCRIPTION, Description);
hm.put(PICTURE, Picture);
hm.put(LINK, Link);
hm.put(ID, Id);
list.add(hm);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item,
new String[]{MESSAGE, NAME, CAPTION, DESCRIPTION },
new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4});
listView.setAdapter(adapter);
}
}
答案 0 :(得分:0)
你应该看第88行(参见Stasktrace:
at com.example.vladzakharo.facebookapp.CentralActivity$2.onCompleted(CentralActivity.java:88)
,你得到的一个字符串:
jArray.getJSONObject(i).get("xxx")
可能返回null,导致NPE。
也可能
jArray.getJSONObject(i)
返回null。