0

当我在应用程序的片段中插入列表视图时,在我用项目填充它后它不会显示。事实上,应用程序由于 NullPointerException 而崩溃。有谁能够帮助我?这是我展示片段的详细活动。

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

我目前正在尝试使用的片段称为分配片段。正如您在 CourseDetailActvity 中看到的那样,我在列表视图中填充了一些项目以查看列表视图是否显示。片段正确膨胀,但是当我尝试将项目添加到列表视图时,应用程序崩溃了!

这是日志猫。

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
4

1 回答 1

0

我相信你的崩溃是在这条线上:

listView.setAdapter(adapter);

adapter显然不为空(崩溃不在其中setAdapter()),所以listView必须为空。

ListView listView = (ListView) findViewById(R.id.assignlist);

好吧,我们可以看到它在那里声明。但是,findViewById()如果找不到对象,则可以返回 null。您正在使用的布局中似乎没有ListViewID 的对象(尤其是 no ) ( )。assignlistactivity_course_detail.xml

于 2012-11-17T17:02:13.103 回答