我花了好几个小时试图调试此代码。基本上我有MainActivity
使用您在启动新项目后获得的默认代码。
这是我的MainActivityFragment
:
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" />
以下是Stack Trace所说的内容:
致命的例外:主要 处理:app.xxxx.github.io.sunshine,PID:4652 java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.TextView.setText(java.lang.CharSequence中)&#39;在null 对象参考
在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:399) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
很抱歉,如果它有点难看,但必须提供足够的问题上下文。感谢。
答案 0 :(得分:2)
将TextView的ID修改为<?count(xdoxslt:distinct_values(CwaProductCode))?>
的声明:
ArrayAdapter
或者,使用内置TextView,如下所示:
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
R.id.list_item_forecast_textview, weatherData);
答案 1 :(得分: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);
答案 2 :(得分: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);
答案 3 :(得分: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);