0

我花了数小时试图调试此代码。基本上我有MainActivity与你开始一个新项目后得到的默认代码。 这是我MainActivityFragment尝试显示字符串列表的奇怪的NullPointerException

package app.xxxx.github.io.sunshine; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.app.Activity; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.Arrays; 
import java.util.List; 
public class MainActivityFragment extends Fragment { 

    public MainActivityFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     String[] fakeData = new String[]{ 
       "Today - Sunny - 88/63", 
       "Tomorrow - Foggy - 70/46", 
       "Weds - Cloudy - 72/63", 
       "Thurs - Rainy - 64/51", 
       "Fri - Foggy - 70/46", 
       "Sat - Sunny - 76/68" 
     }; 
     List<String> weatherData = Arrays.asList(fakeData); 
     ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
        R.id.listview_forecast, weatherData); 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast); 
     listview.setAdapter(myAdapter); 
     return rootView;}} 

正如你所看到的,我只是试图表现出一些虚拟串到屏幕上。

这里是FragmentMain.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="app.xxxx.github.io.sunshine.MainActivityFragment" 
    tools:showIn="@layout/activity_main"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listview_forecast" /> 

</FrameLayout> 

list_item_forecast.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:id="@+id/list_item_forecast_textview" /> 

这里是堆栈跟踪说:

致命异常:主要 过程:应用程序。 XXXX。 github.io.sunshine,PID:4652 显示java.lang.NullPointerException:尝试在一个空 对象引用
在 机器人调用虚拟方法 '无效 android.widget.TextView.setText(java.lang.CharSequence中)' .widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:399) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)

很抱歉,如果它得到了一点点的难看,但不得不放弃了足够的上下文问题。谢谢。

+0

您在'ArrayAdapter'构造函数调用中传递错误的'R.id'。它需要是列表项布局中的'TextView'的ID - 'R.id.list_item_forecast_textview' - 或者只是删除该参数,如果布局只是'TextView'。 –

+0

** id **是R.id.list_item_forecast_textview。 –

回答

0

你应该提供list_item_TextView的id,而不是ListView本身。 因此改变你的代码

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
       R.id.list_item_forecast_textview, weatherData); 
0

你的适配器给TextView中的错误ID

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
        R.id.listview_forecast, weatherData); 

所以用

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
        R.id.list_item_forecast_textview, weatherData); 
2

替换该行修正你的TextView的id进入ArrayAdapter声明:

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
         R.id.list_item_forecast_textview, weatherData); 

或者,使用内置的TextView如下:

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
         android.R.id.text1, weatherData); 
0

您正在使用此构造函数创建适配器实例。

/** 
    * Constructor 
    * 
    * @param context The current context. 
    * @param resource The resource ID for a layout file containing a layout to use when 
    *     instantiating views. 
    * @param textViewResourceId The id of the TextView within the layout resource to be populated 
    * @param objects The objects to represent in the ListView. 
    */ 
    public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId, 
      @NonNull T[] objects) { 
     this(context, resource, textViewResourceId, Arrays.asList(objects)); 
    } 

它采用

@param textViewResourceId The id of the TextView within the layout resource to be populated 

由于您使用的自定义布局,所以你必须通过正确的TextView id来填充数据

简单地改变

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
       R.id.listview_forecast, weatherData); 

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, 
       R.id.list_item_forecast_textview, weatherData);