gpt4 book ai didi

java - 空指针异常 - 从 fragment 加载新 Activity

转载 作者:行者123 更新时间:2023-12-01 12:37:44 25 4
gpt4 key购买 nike

当我尝试从 fragment 加载新 Activity 时遇到了空指针异常 - 它本质上告诉我发生了意外错误。以下是日志猫消息:

08-21 11:57:14.801: E/AndroidRuntime(1245): FATAL EXCEPTION: main
08-21 11:57:14.801: E/AndroidRuntime(1245): Process: com.dooba.beta, PID: 1245
08-21 11:57:14.801: E/AndroidRuntime(1245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.matchOptionActivity}: java.lang.NullPointerException
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.os.Looper.loop(Looper.java:136)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-21 11:57:14.801: E/AndroidRuntime(1245): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 11:57:14.801: E/AndroidRuntime(1245): at java.lang.reflect.Method.invoke(Method.java:515)
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-21 11:57:14.801: E/AndroidRuntime(1245): at dalvik.system.NativeStart.main(Native Method)
08-21 11:57:14.801: E/AndroidRuntime(1245): Caused by: java.lang.NullPointerException
08-21 11:57:14.801: E/AndroidRuntime(1245): at com.dooba.beta.matchOptionActivity.onCreate(matchOptionActivity.java:29)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.Activity.performCreate(Activity.java:5231)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-21 11:57:14.801: E/AndroidRuntime(1245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-21 11:57:14.801: E/AndroidRuntime(1245): ... 11 more

下面是fragment Activity代码(调用新intent Activity的地方)

public class Fragment1 extends Fragment {

private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ListView usersListView;
private Button logoutButton;
String userGender = ParseUser.getCurrentUser().getString("Gender");
String activityName = ParseUser.getCurrentUser().getString("ActivityName");
Number maxDistance = ParseUser.getCurrentUser().getNumber("Maximum_Distance");
String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");
Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
Number userage = ParseUser.getCurrentUser().getNumber("Age");

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

setConversationsList();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
Button newPage = (Button)view.findViewById(R.id.btnMatchConfirm);
newPage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), matchOptionActivity.class);
startActivity(intent);
}
});


return view;
}

private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
// String userActivitySelectionName = null;

ParseQuery<ParseUser> query = ParseUser.getQuery();

// query.whereEqualTo("ActivityName",userActivitySelectionName);

query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
// users with Gender = currentUser.Looking_Gender
query.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query.whereEqualTo("Looking_Gender", userGender);
query.setLimit(1);
query.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);
query.orderByDescending("Name");




query.findInBackground(new FindCallback<ParseUser>() {

public void done(List<ParseUser> userList, ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).get("Name").toString());
names.add(userList.get(i).get("Headline").toString());



names.add(userList.get(i).get("Age").toString());
names.add(userList.get(i).get("ActivityName").toString());


// names.add(userList.get(i).getParseObject("ProfilePicture").;


}




usersListView = (ListView)getActivity().findViewById(R.id.userlistview1);
namesArrayAdapter =
new ArrayAdapter<String>(getActivity().getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);

usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});

} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
}

public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("Name", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getActivity().getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

下面是 matchOption Activity 的代码(单击按钮时调用的 Activity )

   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matchoption);

final ImageView idrinks = (ImageView) this.findViewById(R.id.icasual);
idrinks.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
matchOptionActivity.this.startActivity(new Intent(matchOptionActivity.this, MessagingActivity.class));
}
});


}
}

此外,我正在使用 Parse 填充我的用户列表,我想知道如果列表为空,我将如何隐藏确认按钮。

提前致谢。

更新下面是 fragment XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_bac3"
android:orientation="vertical" >

<ListView
android:id="@+id/userlistview1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:textColor="#ffffff"
android:divider="@null"

>
</ListView>

<Button
android:id="@+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/gray_bac"
android:layout_below="@+id/userlistview1"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:text="Confirm" />

</RelativeLayout>

最佳答案

我没有权限添加评论...您能否提供 matchoption xml 以供进一步分析或检查 onClick() 方法。看起来那里有些问题

引起:java.lang.NullPointerException08-21 11:57:14.801: E/AndroidRuntime(1245): 在 com.dooba.beta.matchOptionActivity.onCreate(matchOptionActivity.java:29)

关于java - 空指针异常 - 从 fragment 加载新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25426830/

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