我知道这是一个很常见的问题,但我已经四处搜寻,我尝试解决以下问题的尝试都没有成功。
特别是,以下是我从logcat收到的消息。
08-03 17:55:53.885: E/AndroidRuntime(1286): FATAL EXCEPTION: main
08-03 17:55:53.885: E/AndroidRuntime(1286): Process: com.dooba.beta, PID: 1286
08-03 17:55:53.885: E/AndroidRuntime(1286): java.lang.RuntimeException: Unable to resume activity {com.dooba.beta/com.dooba.beta.MatchingActivity}: java.lang.NullPointerException
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.os.Handler.dispatchMessage(Handler.java:102)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.os.Looper.loop(Looper.java:136)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-03 17:55:53.885: E/AndroidRuntime(1286): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 17:55:53.885: E/AndroidRuntime(1286): at java.lang.reflect.Method.invoke(Method.java:515)
08-03 17:55:53.885: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-03 17:55:53.885: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-03 17:55:53.885: E/AndroidRuntime(1286): at dalvik.system.NativeStart.main(Native Method)
08-03 17:55:53.885: E/AndroidRuntime(1286): Caused by: java.lang.NullPointerException
08-03 17:55:53.885: E/AndroidRuntime(1286): at com.dooba.beta.MatchingActivity.onResume(MatchingActivity.java:82)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.Activity.performResume(Activity.java:5310)
08-03 17:55:53.885: E/AndroidRuntime(1286): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
08-03 17:55:53.885: E/AndroidRuntime(1286): ... 12 more
以下是活动的代码
public class MatchingActivity extends Activity {
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected List<ParseUser> mUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.matching);
// Show the Up button in the action bar.
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> users, ParseException e) {
if (e == null) {
mUsers = users;
} else {
// Something went wrong.
}
}
});
//This give you a list of users. Then to access one user you need to know the index the user is at.
ParseUser user = mUsers.get(5);
//then you can access that user's information
user.getString("name");
user.getNumber("age");
user.getString("headline");
}
}
以下是xml的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
非常感谢任何帮助。 提前谢谢。
更新了代码: 收到错误 为行
mUsers = new List<ParseUser>();
我收到以下消息:
&#34;无法实例化类型List&lt; ParseUser&gt;&#34;
为行
user.getString("name");
user.getNumber("age");
user.getString("headline");
我收到以下错误:
&#34;用户无法解析&#34;
提前致谢,以下是完整的更新代码。
public class MatchingActivity extends Activity {
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected List<ParseUser> mUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.matching);
// Show the Up button in the action bar.
//create list variable
mUsers = new List<ParseUser>();
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> users, ParseException e) {
if (e == null) {
//add all the users to your list variable
mUsers.addAll(users);
} else {
// Something went wrong.
}
}
});
//check the size of your list to see how big it is before accessing it
final int size = mUsers.size();
//or use a loop to loop through each one
for(ParseUser mParseUser : mUsers) {
//skip over the current user
if(mParseUser == ParseUser.getCurrentUser())
continue;
user.getString("name");
user.getNumber("age");
user.getString("headline");
}
}
}
答案 0 :(得分:1)
我认为您的问题出现在以下代码行中
ParseUser user = mUsers.get(5);
mUsers.get(5)可能为null,或者不存在。您的代码中没有任何内容可以保证列表必须至少为5。
试试这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.matching);
//create list variable
mUsers = new List<ParseUser>();
}
@Override
protected void onResume() {
...
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>()
{
public void done(List<ParseUser> users, ParseException e)
{
if (e == null)
{
//add all the users to your list variable
mUsers.addAll(users);
//check the size of your list to see how big it is before accessing it
final int size = mUsers.size();
//or use a loop to loop through each one
for(ParseUser mParseUser : mUsers)
{
//skip over the current user
if(mParseUser == ParseUser.getCurrentUser())
continue;
//use the correct type when getting
final String mName = user.getString("name");
final int mNumber = user.getNumber("age");
final String mHeadLine = user.getString("headline");
}
}
else
{
// Something went wrong.
}
}
});
}