声明textView会在textView上显示NullPointerException

时间:2015-11-29 19:03:17

标签: android listview textview

当“附近的地方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>

1 个答案:

答案 0 :(得分:3)

根据您的代码帖子,您将从活动中获取import Foundation import AppKit final class TestApplicationController: NSObject, NSApplicationDelegate { let window1 = NSWindow() let view = NSView() func applicationDidFinishLaunching(aNotification: NSNotification) { window1.setFrame(CGRect(x: 0, y: 0, width: 443, height: 229), display: true) window1.contentView = view window1.opaque = false window1.center() window1.makeKeyAndOrderFront(self) window1.title = "Finder" window1.level = Int(CGWindowLevelForKey(.MaximumWindowLevelKey)) window1.makeKeyAndOrderFront(self) // MARK: - Name Label let nameLabel: NSTextField = NSTextField(frame: NSMakeRect(138, 101, 44, 17)) nameLabel.editable = false nameLabel.drawsBackground = false nameLabel.bezeled = false nameLabel.selectable = false nameLabel.stringValue = "Name:" windowContentView.addSubview(nameLabel) // MARK: - Password Label let passwordLabel: NSTextField = NSTextField(frame: NSMakeRect(118, 69, 64, 17)) passwordLabel.editable = false passwordLabel.drawsBackground = false passwordLabel.bezeled = false passwordLabel.selectable = false passwordLabel.stringValue = "Passwort:" windowContentView.addSubview(passwordLabel) // MARK: - Name Field let nameField: NSTextField = NSTextField(frame: NSMakeRect(190, 98, 233, 22)) nameField.editable = true nameField.selectable = true nameField.stringValue = "Michael" nameField.action = "nameEdited:" windowContentView.addSubview(nameField) // MARK: - Password Field let passwordField: NSSecureTextField = NSSecureTextField(frame: NSMakeRect(190, 66, 233, 22)) passwordField.editable = true passwordField.selectable = true passwordField.stringValue = "" passwordField.becomeFirstResponder() passwordField.action = "passwordEdited:" windowContentView.addSubview(passwordField) func nameEdited(sender: NSTextField){ print("editedname") } func passwordEdited(sender: NSTextField){ print("editedpw") } } 的句柄。但是您的活动文件中没有TextView。它在您的布局中用于自定义适配器。所以你需要处理适配器类中的东西。

在适配器的TextView方法中,基于您的案例中的模型列表值,它是getView()。 在此pojo类中维护关于评级的属性,在getview中,您将拥有测试视图的句柄以及此模型类,您可以在那里执行任务。