当来自“附近地点api”的评级小于或等于3为红色,大于3为绿色时,我尝试更改textView颜色,但我得到的NullPointerException如下所示:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference
下面是我的代码的一部分,我知道声明textView不在正确的位置。但是,我试图在onCreate中声明它,但是我得到了相同的错误。
活动的一部分:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_activity);
myList = (ListView) findViewById(R.id.placesList);
}
public class readFromGooglePlaceAPI extends AsyncTask<String, Void, String> {
@Override protected String doInBackground(String... param) {
return readJSON(param[0]);
}
protected void onPostExecute(String str) {
myArrayList = new ArrayList<GetterSetter>();
String rating=" -NA-";
try {
JSONObject root = new JSONObject(str);
JSONArray results = root.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
addValues = new GetterSetter();
TextView rate = (TextView) findViewById(R.id.rating);
JSONObject arrayItems = results.getJSONObject(i);
if(!arrayItems.isNull("rating")){
rating = arrayItems.getString("rating");
if(Float.parseFloat(rating) <= 3){
rate.setTextColor(Color.RED);
}
else if(Float.parseFloat(rating) > 3){
rate.setTextColor(Color.GREEN);
}
}
addValues.setRating(rating);
myArrayList.add(addValues);
}
} catch (Exception e) {
Log.e("Error", "Exception ..." , e);
}
adapter = new CustomAdapter(ListActivity.this, R.layout.list_row, myArrayList);
myList.setAdapter(adapter);
}
}
list_view_activity.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/placesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:paddingTop="70dp"
android:divider="@drawable/divider"
android:dividerHeight="1px"
android:cacheColorHint="#00000000"
android:listSelector="@drawable/listview_selector">
</ListView>
</LinearLayout>
list_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<TextView
android:id="@+id/ratingRext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignRight="@id/name"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="20dp"
android:text="Rating :"
android:textSize="14sp"
android:textColor="#282f8d" />
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignRight="@id/ratingRext"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textSize="12sp" />
</RelativeLayout>
发布于 2015-11-29 11:10:56
根据您的代码发布,您将从活动中获取TextView
句柄。但是您的活动文件中没有TextView
。它在用于自定义适配器的布局中。因此,您需要处理适配器类中的内容。
在基于模型列表值的适配器的getView()
方法中,在您的示例中,它是myArrayList
。在这个pojo类中维护一个有关评级的属性,在getview中,您将拥有测试视图的句柄以及这个模型类,您可以在那里执行任务。
https://stackoverflow.com/questions/33986762
复制相似问题