方法 getApplicationContext() 未定义 - 片段问题
2022-09-02 02:09:50
我收到以下错误: 方法 getApplicationContext() 未定义 方法 findViewById(int) 未定义类型 Fragment1
当我的类扩展到与片段相对的活动时,这些错误似乎被消除了,但是重要的是,这个活动仍然是一个片段,所以我不太确定如何解决这个问题。
任何帮助将不胜感激。提前致谢。
下面是代码
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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null){
return null;
}
return (LinearLayout)inflater.inflate(R.layout.fragment1_layout,
container,false);
logoutButton = (Button) findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
setConversationsList();
}
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("Minimum_Age", minimumAge).whereGreaterThanOrEqualTo("Age", userage);
query.whereLessThanOrEqualTo("Maximum_Age", maximumAge).whereLessThanOrEqualTo("Age", userage);
// query.whereWithinKilometers("Maximum_Distance", point, maxDistance)
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).getParseObject("ProfilePicture").;
}
usersListView = (ListView)findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(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(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(getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
更新设置冲突列表的无法访问的代码错误
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null){
return null;
}
return (LinearLayout)inflater.inflate(R.layout.fragment1_layout,
container,false);
setConversationsList();
}
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
// String userActivitySelectionName = null;
ParseQuery<ParseUser> query = ParseUser.getQuery();
更新
启动活动后,我收到以下消息:
08-15 14:52:16.365: E/AndroidRuntime(3332): FATAL EXCEPTION: main
08-15 14:52:16.365: E/AndroidRuntime(3332): Process: com.dooba.beta, PID: 3332
08-15 14:52:16.365: E/AndroidRuntime(3332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.usermatch}: java.lang.NullPointerException
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.os.Handler.dispatchMessage(Handler.java:102)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.os.Looper.loop(Looper.java:136)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-15 14:52:16.365: E/AndroidRuntime(3332): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 14:52:16.365: E/AndroidRuntime(3332): at java.lang.reflect.Method.invoke(Method.java:515)
08-15 14:52:16.365: E/AndroidRuntime(3332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-15 14:52:16.365: E/AndroidRuntime(3332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-15 14:52:16.365: E/AndroidRuntime(3332): at dalvik.system.NativeStart.main(Native Method)
08-15 14:52:16.365: E/AndroidRuntime(3332): Caused by: java.lang.NullPointerException
08-15 14:52:16.365: E/AndroidRuntime(3332): at com.dooba.beta.usermatch.initialisePaging(usermatch.java:32)
08-15 14:52:16.365: E/AndroidRuntime(3332): at com.dooba.beta.usermatch.onCreate(usermatch.java:20)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.Activity.performCreate(Activity.java:5231)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-15 14:52:16.365: E/AndroidRuntime(3332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-15 14:52:16.365: E/AndroidRuntime(3332): ... 11 more
下面是调用片段的活动页面
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class usermatch extends FragmentActivity {
private PageAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);
initialisePaging();
}
private void initialisePaging() {
// TODO Auto-generated method stub
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment3.class.getName()));
mPagerAdapter = new PageAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mood, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
更新 2以下是我在 logcat 中收到的消息
08-15 17:42:28.863: E/AndroidRuntime(4974): FATAL EXCEPTION: main
08-15 17:42:28.863: E/AndroidRuntime(4974): Process: com.dooba.beta, PID: 4974
08-15 17:42:28.863: E/AndroidRuntime(4974): java.lang.NullPointerException
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.dooba.beta.Fragment1$1.done(Fragment1.java:108)
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.parse.FindCallback.internalDone(FindCallback.java:45)
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.parse.FindCallback.internalDone(FindCallback.java:1)
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.parse.Parse$6$1.run(Parse.java:888)
08-15 17:42:28.863: E/AndroidRuntime(4974): at android.os.Handler.handleCallback(Handler.java:733)
08-15 17:42:28.863: E/AndroidRuntime(4974): at android.os.Handler.dispatchMessage(Handler.java:95)
08-15 17:42:28.863: E/AndroidRuntime(4974): at android.os.Looper.loop(Looper.java:136)
08-15 17:42:28.863: E/AndroidRuntime(4974): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-15 17:42:28.863: E/AndroidRuntime(4974): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 17:42:28.863: E/AndroidRuntime(4974): at java.lang.reflect.Method.invoke(Method.java:515)
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-15 17:42:28.863: E/AndroidRuntime(4974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-15 17:42:28.863: E/AndroidRuntime(4974): at dalvik.system.NativeStart.main(Native Method)