我有一个listview
填充了relativelayouts
,它们自己拥有控件。问题是我在活动UI完全加载之前无法访问这些项目(我得到null pointer exception
。没有OnActivityLoaded()
方法,这就是我尝试做的事情:
OnStart()
和OnResume()
; Adapter
,但我需要使用一些功能
是主要的活动,所以这是一个禁忌; Handler.postDelayed
访问给定数量的元素后的元素
时间。这适用于高端设备,但在低端设备上有延迟
时间不足以加载(即使我将其设置为高达1000
ms)所以我仍然得到null pointer exception
。因此,我需要一种更好,更恰当的方式来访问主要活动中的小部件。
有什么想法?有没有办法知道UI何时完全显示?
编辑:这是我在加载活动后需要访问的代码。
// These 4 widgets are ones from the relativelayout which is an item in the listview
homeSemester = (Spinner) findViewById(R.id.home_profile_semester);
homeSpecialty = (Spinner) findViewById(R.id.home_profile_specialty);
homefbPP = (ProfilePictureView) findViewById(R.id.home_profile_fbPP);
homefbName = (TextView) findViewById(R.id.home_profile_fbName);
// On click listeners
homeSemester.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// These functions need to be in the main activity (have access to widgets, savedInstance bundles and shared preferences
// Configure specialty
configSpecialty();
// Load Schedule
loadSchedule();
// Refresh contact list
loadContacts();
configurePush();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplication(), "Please select your Semester!", Toast.LENGTH_SHORT).show();
Log.e("Initialization", "No semester selected! This should not happen!");
}
});
homeSpecialty.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// Load schedule
loadSchedule();
// Refresh contact list
loadContacts();
// Save Data
saveData();
configurePush();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Log.e("Initialization", "No specialty selected! This should not happen!");
}
});
// All of these depend on homesemester and homespecialty
// Read data
readSemester();
readSpecialty();
// Show all button
contactAll.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if(contactAll.isChecked())
loadAll();
else
loadContacts();
}
});
if(contactAll.isChecked())
loadAll();
else
loadContacts();
loadSchedule();
if (sharedPref.getInt("LoggedIn", 0) == 0)
{
// Actions taken on buttons click
fbLogin.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (fbLogin.getText().toString().equals("Log in with Facebook"))
{
// Welcome Message
ab.setSubtitle("Logging in...");
// Login to Facebook
NetAvailable = isNetworkAvailable();
if (NetAvailable)
{
logIn();
}
else
{
ab.setSubtitle("No Internet Connection!");
}
editor.putInt("LoggedIn", 1);
editor.commit();
}
else
{
// Log Out
AlertDialog.Builder builderfb = new AlertDialog.Builder(Launch.this);
builderfb.setTitle("Log Out")
.setMessage("Are you sure do you want to Log Out?\n\nNote: Logging out from Facebook will close the application. Next time you start ULFG2 App2Date, you will be prompted to Log in manually.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
editor.putInt("LoggedIn", 0);
editor.commit();
finish();
}
});
AlertDialog welcomeMsg = builderfb.create();
welcomeMsg.show();
}
}
});
}
else
{
fbLogin.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// Log Out only is needed
AlertDialog.Builder builderfb = new AlertDialog.Builder(Launch.this);
builderfb.setTitle("Log Out")
.setMessage("Are you sure do you want to Log Out?\n\nNote: Logging out from Facebook will close the application. Next time you start ULFG2 App2Date, you will be prompted to Log in manually.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
editor.putInt("LoggedIn", 0);
editor.commit();
finish();
}
});
AlertDialog welcomeMsg = builderfb.create();
welcomeMsg.show();
}
});
}
homefbName.setText(sharedPref.getString("student_name", "Student"));
homefbPP.setProfileId(sharedPref.getString("student_id", ""));
}
在onCreate()
:
ab = getSupportActionBar();
ab.setTitle("App2Date");
abDL = (DrawerLayout) findViewById(R.id.MainLayout);
abLV = (ListView) findViewById(R.id.list_slidermenu);
LayoutInflater mInflater = (LayoutInflater) getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
navDrawerItems = new ArrayList<View>();
navDrawerItems.add(mInflater.inflate(R.layout.drawer_profile, null));
adapter = new NDListAdapter(getApplicationContext(), navDrawerItems);
abLV.setAdapter(adapter);
abLV.setDivider(null);
abLV.setDividerHeight(0);
// setting the nav drawer list adapter
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
abDT = new ActionBarDrawerToggle(this, abDL,
R.drawable.ic_navigation_drawer,
R.string.drawer_open,
R.string.drawer_close)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(R.string.drawer_close);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.drawer_open);
}
};
abDL.setDrawerListener(abDT);
abDT.setDrawerIndicatorEnabled(true);
如果我调用第一行代码(在编辑下方),我会在null pointer exception
中获得onCreate()