gpt4 book ai didi

java - NullPointerException - fragment

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

在尝试运行我的应用程序时,我收到了来自 logcat 的以下消息。本质上,我试图使用查询从解析中检索信息列表。代码看起来不错,所以我不太确定如何解决这个问题。

Logcat消息

08-24 19:23:35.387: E/AndroidRuntime(3996): FATAL EXCEPTION: main
08-24 19:23:35.387: E/AndroidRuntime(3996): Process: com.dooba.beta, PID: 3996
08-24 19:23:35.387: E/AndroidRuntime(3996): java.lang.NullPointerException
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.dooba.beta.Fragment1$2.done(Fragment1.java:154)
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.parse.FindCallback.internalDone(FindCallback.java:45)
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.parse.FindCallback.internalDone(FindCallback.java:1)
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.parse.Parse$6$1.run(Parse.java:888)
08-24 19:23:35.387: E/AndroidRuntime(3996): at android.os.Handler.handleCallback(Handler.java:733)
08-24 19:23:35.387: E/AndroidRuntime(3996): at android.os.Handler.dispatchMessage(Handler.java:95)
08-24 19:23:35.387: E/AndroidRuntime(3996): at android.os.Looper.loop(Looper.java:136)
08-24 19:23:35.387: E/AndroidRuntime(3996): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-24 19:23:35.387: E/AndroidRuntime(3996): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 19:23:35.387: E/AndroidRuntime(3996): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-24 19:23:35.387: E/AndroidRuntime(3996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-24 19:23:35.387: E/AndroidRuntime(3996): at dalvik.system.NativeStart.main(Native Method)

代码

public class Fragment1 extends Fragment {

public interface Constants {
String LOG = "com.dooba.beta";
}

private String currentUserId;
private String currentUser;

protected TextView mUserRetrieved;
protected TextView mUserActivityNameRetrieved;
protected TextView mUserNameRetrieved;




private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ArrayList<String> age;
private ArrayList<String> headline;
private ArrayList<String> activityname;

private ArrayList<Images> alProfilePicture;
private ListView usersListName;
private ListView usersListHeadline;
private ListView usersListActivityname;

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);

mUserRetrieved = (TextView) getActivity().findViewById(R.id.userlistname);


Button newPage = (Button)view.findViewById(R.id.btnMatchConfirm);
newPage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();

currentUser.put("UserMatchName", mUserRetrieved);
Intent intent = new Intent(getActivity(), matchOptionActivity.class);
startActivity(intent);
}
});


return view;
}

private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
age = new ArrayList<String>();
headline = new ArrayList<String>();
activityname = new ArrayList<String>();

alProfilePicture = new ArrayList<Images>();

ParseQuery<ParseUser> query = ParseUser.getQuery();
ParseQuery<ParseUser> query2 = ParseUser.getQuery();
ParseQuery<ParseUser> query3 = ParseUser.getQuery();
ParseQuery<ParseUser> query4 = ParseUser.getQuery();
ParseQuery<ParseUser> query5 = ParseUser.getQuery();

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

@Override
public void done(List<ParseUser> objects,ParseException e) {

for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
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);

mUserActivityNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistactivityname);

mUserActivityNameRetrieved.setText(objects.get(i).get("ActivityName").toString());



}
}//for loop
});

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

@Override
public void done(List<ParseUser> objects,ParseException e) {

for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query2 = ParseQuery.getQuery("User");
query2.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
.getObjectId());
// users with Gender = currentUser.Looking_Gender
query2.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query2.whereEqualTo("Looking_Gender", userGender);
query2.setLimit(1);
query2.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);

mUserNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistname);

mUserNameRetrieved.setText(objects.get(i).get("Name").toString());



}
}//for loop
});

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

@Override
public void done(List<ParseUser> objects,ParseException e) {

for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query3 = ParseQuery.getQuery("User");
query3.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
.getObjectId());
// users with Gender = currentUser.Looking_Gender
query3.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query3.whereEqualTo("Looking_Gender", userGender);
query3.setLimit(1);
query3.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);

mUserNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistage);

mUserNameRetrieved.setText(objects.get(i).get("Name").toString());



}
}//for loop
});

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

@Override
public void done(List<ParseUser> objects,ParseException e) {

for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query4 = ParseQuery.getQuery("User");
query4.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
.getObjectId());
// users with Gender = currentUser.Looking_Gender
query4.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query4.whereEqualTo("Looking_Gender", userGender);
query4.setLimit(1);
query4.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);

mUserNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistheadline);

mUserNameRetrieved.setText(objects.get(i).get("Headline").toString());



}
}//for loop
});







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

@Override
public void done(List<ParseUser> objects,ParseException e) {

for(int i=0;i<objects.size();i++){
// Do whatever you need to extract object from "users"
ParseQuery<ParseObject> query5 = ParseQuery.getQuery("User");
query5.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
.getObjectId());
// users with Gender = currentUser.Looking_Gender
query5.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query5.whereEqualTo("Looking_Gender", userGender);
query5.setLimit(1);
query5.whereEqualTo("ActivityName", activityName);
// query.whereGreaterThanOrEqualTo("Age", minimumAge);
// query.whereLessThanOrEqualTo("Age", maximumAge);

ParseFile image = objects.get(i).getParseFile("ProfilePicture");


ParseImageView imageView = (ParseImageView) getView().findViewById(R.id.profilePictureresult);

// The placeholder will be used before and during the fetch, to be replaced by the fetched image
// data.
imageView.setPlaceholder(getResources().getDrawable(R.drawable.profile_pict));
imageView.setParseFile(image);
imageView.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if (data != null) {
Log.i("ParseImageView", "Fetched: " + data.length);
} else {
Log.e("ParseImageView", "Error fetching: " + e.getMessage());
}
}


});
}
}//for loop
});

}


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();
}
}
});
}

}

XML 布局

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

<ImageView
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/profilePictureresult"
android:layout_marginTop="16dp"
android:alpha="0.7"
android:src="@drawable/left_right" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView2"
android:alpha="0.7"
android:src="@drawable/left_arrow2" />

<TextView
android:id="@+id/userlistactivityname"
android:textColor="#ADD8E6"
android:padding="6dp"
android:gravity="center"
android:textStyle="bold"
android:typeface="serif"
android:layout_below="@+id/tactivityname"
android:layout_centerHorizontal="true"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" >
</TextView>

<TextView
android:id="@+id/userlistage"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_below="@+id/profilePictureresult"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:divider="@null" >
</TextView>

<TextView
android:id="@+id/tactivityname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/userlistage"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:text="Activity Name"
android:textColor="#d2f2a2"
android:textSize="23sp"
android:textStyle="bold" />

<com.parse.ParseImageView
android:id="@+id/profilePictureresult"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_below="@+id/userlistheadline"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:alpha="1"
android:background="@drawable/border_image"
android:cropToPadding="true"
android:padding="3dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/userlistheadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/userlistname"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="6dp"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif" />

<TextView
android:id="@+id/userlistname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/userlistheadline"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:gravity="center"
android:padding="6dp"
android:textColor="#ADD8E6"
android:textSize="22sp"
android:textStyle="bold"
android:typeface="serif" />

<Button
android:id="@+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="@+id/userlistactivityname"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:alpha="0.7"
android:background="#ADD8E6"
android:text="Confirm"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="serif" />

</RelativeLayout>

提前致谢

最佳答案

在每个 done() 回调中,您正在执行以下操作:

mUserNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistage);

这将在您的 Activity 中搜索 id userlistage
但是,这个 id 位于您的 Fragment 中,而不是您的 Activity 中。
改为这样做:

mUserNameRetrieved = (TextView) getView().findViewById(R.id.userlistage);

如果您的 fragment 在任务期间被破坏,请小心。

关于java - NullPointerException - fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475747/

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