- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写此代码以在 android 中拥有自定义列表。我正在使用
'com.android.support:support-v4:26.1.0'
我当前的代码试图显示包含图像和文本的 listview
,但我想这无关紧要。
FourthFragment.java
public class FourthFragment extends ListFragment {
private ArrayList<Weather> weatherProperties = new ArrayList<>();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_fourth, container, false);
//create our property elements
weatherProperties.add(new Weather(10, "Smith Street", "Sydney", "NSW", "A large 3 bedroom apartment right in the heart of Sydney! A rare find, with 3 bedrooms and a secured car park.", 450.00, "property_image_1", 3, 1, 1));
weatherProperties.add(new Weather(66, "King Street", "Sydney", "NSW", "A fully furnished studio apartment overlooking the harbour. Minutes from the CBD and next to transport, this is a perfect set-up for city living.", 320.00, "property_image_2", 1, 1, 1));
weatherProperties.add(new Weather(1, "Liverpool Road", "Liverpool", "NSW", "A standard 3 bedroom house in the suburbs. With room for several cars and right next to shops this is perfect for new families.", 360.00, "property_image_3", 3, 2, 2));
weatherProperties.add(new Weather(567, "Sunny Street", "Gold Coast", "QLD", "Come and see this amazing studio apartment in the heart of the gold coast, featuring stunning waterfront views.", 360.00, "property_image_4" , 1, 1, 1));
TextView tv = (TextView) myFragmentView.findViewById(R.id.bedroom);
tv.setText("My Header");
// setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
// New Array Adapters
ArrayAdapter<Weather> adapter = new weatherArrayAdapter(getContext(), 0, weatherProperties);
//Find list view and bind it with the custom adapter
ListView listView = (ListView) tv.findViewById(R.id.customListView);
listView.setAdapter(adapter);
return myFragmentView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// on click display the item in toast
Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
}
static FourthFragment newInstance(int num) {
FourthFragment fourthFragment = new FourthFragment();
Bundle args = new Bundle();
args.putInt("num", num);
fourthFragment.setArguments(args);
return fourthFragment;
}
//custom ArrayAdapater
class weatherArrayAdapter extends ArrayAdapter<Weather>{
private Context context;
private List<Weather> weatherProperties;
//constructor, call on creation
public weatherArrayAdapter(Context context, int resource, ArrayList<Weather> objects) {
super(context, resource, objects);
this.context = context;
this.weatherProperties = objects;
}
//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {
//get the weather we are displaying
Weather weather = weatherProperties.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//conditionally inflate either standard or special template
View view;
view = inflater.inflate(R.layout.weather_layout, null);
TextView description = (TextView) view.findViewById(R.id.description);
TextView address = (TextView) view.findViewById(R.id.address);
TextView bedroom = (TextView) view.findViewById(R.id.bedroom);
TextView bathroom = (TextView) view.findViewById(R.id.bathroom);
TextView carspot = (TextView) view.findViewById(R.id.carspot);
TextView price = (TextView) view.findViewById(R.id.price);
ImageView image = (ImageView) view.findViewById(R.id.image);
//set address and description
String completeAddress = weather.getStreetNumber() + " " + weather.getStreetName() + ", " + weather.getSuburb() + ", " + weather.getState();
address.setText(completeAddress);
//display trimmed excerpt for description
int descriptionLength = weather.getDescription().length();
if(descriptionLength >= 100){
String descriptionTrim = weather.getDescription().substring(0, 100) + "...";
description.setText(descriptionTrim);
}else{
description.setText(weather.getDescription());
}
//set price and weather attributes
price.setText("$" + String.valueOf(weather.getPrice()));
bedroom.setText("Bed: " + String.valueOf(weather.getBedrooms()));
bathroom.setText("Bath: " + String.valueOf(weather.getBathrooms()));
carspot.setText("Car: " + String.valueOf(weather.getCarspots()));
//get the image associated with this weather
int imageID = context.getResources().getIdentifier(weather.getImage(), "drawable", context.getPackageName());
image.setImageResource(imageID);
return view;
}
}
}
没有 compiletime
错误,但这给了我 runtime
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.phocast.FourthFragment.onCreateView(FourthFragment.java:39)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
我做错了什么?
如果你能把问题告诉我,我将不胜感激。
回答和评论后更新
错误在这里:(很抱歉把它作为引用,否则不允许我提交主要是代码错误
)
TextView tv = (TextView) myFragmentView.findViewById(R.id.bedroom);
tv.setText("My Header");
xml
是:
weather_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:contentDescription="Property Image" />
<LinearLayout
android:id="@+id/infoSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:orientation="vertical">
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Street Address"
android:textSize="18sp" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Description"
android:textSize="15sp" />
</LinearLayout>
<RelativeLayout
android:id="@+id/pricingSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/infoSection"
android:orientation="vertical">
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Price" />
<TextView
android:id="@+id/bedroom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/price"
android:layout_marginRight="15dp"
android:text="Bed:" />
<TextView
android:id="@+id/bathroom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/price"
android:layout_marginRight="15dp"
android:layout_toRightOf="@id/bedroom"
android:text="Bath:" />
<TextView
android:id="@+id/carspot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/price"
android:layout_marginRight="15dp"
android:layout_toRightOf="@id/bathroom"
android:text="Car:" />
</RelativeLayout>
</RelativeLayout>
更新 2:fragment_four.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryLight">
<ListView
android:id="@+id/customListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
android:textAlignment="gravity">
</ListView>
</RelativeLayout>
更新 3所以,我创建了一个 fourth_fragment.xml 作为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimaryLight">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Description"
android:textSize="15sp" />
<ListView
android:id="@+id/customListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:scrollbars="vertical"
android:textAlignment="gravity"></ListView>
</LinearLayout>
并更改了行:
tv = (TextView) myFragmentView.findViewById(R.id.textView);
tv.setText("My Header");
但我仍然收到错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
最佳答案
您将获得 null
在这里。检查你是否有TextView id
= bedroom
或不在布局fragment_fourth
TextView tv = (TextView) myFragmentView.findViewById(R.id.bedroom);
tv.setText("My Header");
您的错误:
'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
它的问题Id
你的TextView
,变量tv
可能是 null
你调用setText
方法null
,这是你做不到的。
并且像这样修改您的代码:
TextView tv; //Global Variable
tv = (TextView) myFragmentView.findViewById(R.id.bedroom);
tv.setText("My Header");
也试试:清理你的项目并重建它。
更改您的 fragmentfour.xml 代码:(如果需要,输入 ScrollView
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimaryLight">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/yourTextView_layout)" />
<ListView
android:id="@+id/customListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
android:textAlignment="gravity">
</ListView>
</LinearLayout>
更新您的 Fragment Java 类:
ListView listView;//globally
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_fourth, container, false);
listView = (ListView) tv.findViewById(R.id.customListView);
//create our property elements
weatherProperties.add(new Weather(10, "Smith Street", "Sydney", "NSW", "A large 3 bedroom apartment right in the heart of Sydney! A rare find, with 3 bedrooms and a secured car park.", 450.00, "property_image_1", 3, 1, 1));
weatherProperties.add(new Weather(66, "King Street", "Sydney", "NSW", "A fully furnished studio apartment overlooking the harbour. Minutes from the CBD and next to transport, this is a perfect set-up for city living.", 320.00, "property_image_2", 1, 1, 1));
weatherProperties.add(new Weather(1, "Liverpool Road", "Liverpool", "NSW", "A standard 3 bedroom house in the suburbs. With room for several cars and right next to shops this is perfect for new families.", 360.00, "property_image_3", 3, 2, 2));
weatherProperties.add(new Weather(567, "Sunny Street", "Gold Coast", "QLD", "Come and see this amazing studio apartment in the heart of the gold coast, featuring stunning waterfront views.", 360.00, "property_image_4" , 1, 1, 1));
tv = (TextView) myFragmentView.findViewById(R.id.bedroom);
tv.setText("My Header");
// setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
// New Array Adapters
ArrayAdapter<Weather> adapter = new weatherArrayAdapter(getContext(), 0, weatherProperties);
//Find list view and bind it with the custom adapter
listView.setAdapter(adapter);
return myFragmentView;
}
关于java - 扩展 Listadapter 时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802921/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!