gpt4 book ai didi

java - 为 ListView 设置适配器时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 14:43:30 25 4
gpt4 key购买 nike

我需要有关第一次滑动 LayoutListView 的帮助...我会在 ListView 中显示一些文本,但它不起作用:(

MainActivity.java:

package com.evolutio.blocklaunch;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;

private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three

// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );

// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );

// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.news, planetList);

// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );

// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);

// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
// Show 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
int tabLayout = 0;
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}

/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {
}

public static final String ARG_OBJECT = "object";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);

int tabLayout = 0;
switch (position) {
case 0:
tabLayout = R.layout.news;
break;
case 1:
tabLayout = R.layout.news;
break;
case 2:
tabLayout = R.layout.news;
break;
}

View rootView = inflater.inflate(tabLayout, container, false);
return rootView;
}
}

}
<小时/>

XML 文件

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />

</android.support.v4.view.ViewPager>

Item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

News.xml

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

<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainListView">
</ListView>
</LinearLayout>

以及错误:

03-29 10:01:39.827: D/AndroidRuntime(6902): Shutting down VM
03-29 10:01:39.827: W/dalvikvm(6902): threadid=1: thread exiting with uncaught exception (group=0x2b521930)
03-29 10:01:39.827: E/AndroidRuntime(6902): FATAL EXCEPTION: main
03-29 10:01:39.827: E/AndroidRuntime(6902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evolutio.blocklaunch/com.evolutio.blocklaunch.MainActivity}: java.lang.NullPointerException
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread.access$600(ActivityThread.java:153)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.os.Looper.loop(Looper.java:137)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread.main(ActivityThread.java:5226)
03-29 10:01:39.827: E/AndroidRuntime(6902): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 10:01:39.827: E/AndroidRuntime(6902): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 10:01:39.827: E/AndroidRuntime(6902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-29 10:01:39.827: E/AndroidRuntime(6902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-29 10:01:39.827: E/AndroidRuntime(6902): at dalvik.system.NativeStart.main(Native Method)
03-29 10:01:39.827: E/AndroidRuntime(6902): Caused by: java.lang.NullPointerException
03-29 10:01:39.827: E/AndroidRuntime(6902): at com.evolutio.blocklaunch.MainActivity.onCreate(MainActivity.java:74)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.Activity.performCreate(Activity.java:5104)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-29 10:01:39.827: E/AndroidRuntime(6902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
03-29 10:01:39.827: E/AndroidRuntime(6902): ... 11 more
03-29 10:01:42.327: I/Process(6902): Sending signal. PID: 6902 SIG: 9

最佳答案

您对 MainActivity 使用了错误的布局,因为您已在 News.xml 中添加了 ListView,但设置了 activity_main对于 MainActivity 。您应该使用 MainActivity 的 News.xml 布局来初始化 mainListView,其中包含 mainListView ListView:

setContentView(R.layout.News);

关于java - 为 ListView 设置适配器时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15700232/

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