问题
When I insert a listview in a fragment in my application, it doesn't show up after I populate it with items. In fact, the application crashes due to a NullPointerException. Can anybody help me? Here is the detail activity from which I show the fragments.
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
/**
* An activity representing a single Course detail screen. This activity is only
* used on handset devices. On tablet-size devices, item details are presented
* side-by-side with a list of items in a {@link CourseListActivity}.
* <p>
* This activity is mostly just a 'shell' activity containing nothing more than
* a {@link CourseDetailFragment}.
*/
public class CourseDetailActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_detail);
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// initiating both tabs and set text to it.
ActionBar.Tab assignTab = actionBar.newTab().setText("Assignments");
ActionBar.Tab schedTab = actionBar.newTab().setText("Schedule");
ActionBar.Tab contactTab = actionBar.newTab().setText("Contact");
// Create three fragments to display content
Fragment assignFragment = new Assignments();
Fragment schedFragment = new Schedule();
Fragment contactFragment = new Contact();
assignTab.setTabListener(new MyTabsListener(assignFragment));
schedTab.setTabListener(new MyTabsListener(schedFragment));
contactTab.setTabListener(new MyTabsListener(contactFragment));
actionBar.addTab(assignTab);
actionBar.addTab(schedTab);
actionBar.addTab(contactTab);
ListView listView = (ListView) findViewById(R.id.assignlist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this,
CourseListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public Fragment fragment2;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.main_across, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}
The fragment that I am currently trying to get working is called the Assignments fragment. As you can see in the CourseDetailActvity, I populate smaple items in the listview to see if it the listview shows up. The fragment gets inflated properly, but when I try to add items to the listview, the application crashes!
Here is the logcat.
11-17 11:54:28.037: E/AndroidRuntime(282): FATAL EXCEPTION: main
11-17 11:54:28.037: E/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sample/com.example.sample.CourseDetailActivity}: java.lang.NullPointerException
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-17 11:54:28.037: E/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 11:54:28.037: E/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521)
11-17 11:54:28.037: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-17 11:54:28.037: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-17 11:54:28.037: E/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method)
11-17 11:54:28.037: E/AndroidRuntime(282): Caused by: java.lang.NullPointerException
11-17 11:54:28.037: E/AndroidRuntime(282): at com.example.sample.CourseDetailActivity.onCreate(CourseDetailActivity.java:66)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 11:54:28.037: E/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-17 11:54:28.037: E/AndroidRuntime(282): ... 11 more
回答1:
I believe your crash is on this line:
listView.setAdapter(adapter);
adapter obviously is not null (the crash is not within setAdapter() anyway), so listView must be null.
ListView listView = (ListView) findViewById(R.id.assignlist);
Well, we can see it declared there. However, findViewById() can return null if the object is not found. There appears to be no object (and in particular, no ListView) with ID assignlist within the layout you are using (activity_course_detail.xml).
来源:https://stackoverflow.com/questions/13432658/listview-not-showing-up-in-fragment