3

I am building an application that parses XML into a ListView. The ListView displays 'Issues' and the severity levels of those issues.

In the XML file 'severityID' is either 1, 2 or 3 but using the 'replace()' I have changed this in my code to High, Medium, and Low (Probably not relevant).

When the TextView says 'High' I want to setTextColor to red and when 'Medium' to yellow etc.

I thought this would be easy using an if statement like:

if (severity.getText().toString.equalsIgnoreCase("High")) {

    severity.setTextColor(Color.RED);

}

However my application force closes.

I have also tried:

if (value.equals("High")) {


 severity.setTextColor(Color.RED);

}    

I guess I'm missing something glaringly obvious here but I have only been developing 3 months so forgive me if I am.

Thanks in advance.

LogCat:

09-10 14:15:37.720: E/AndroidRuntime(6642): FATAL EXCEPTION: main
09-10 14:15:37.720: E/AndroidRuntime(6642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.issueslist/com.example.issueslist.MainActivity}: java.lang.NullPointerException
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.os.Looper.loop(Looper.java:137)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread.main(ActivityThread.java:5289)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at java.lang.reflect.Method.invoke(Method.java:525)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at dalvik.system.NativeStart.main(Native Method)
09-10 14:15:37.720: E/AndroidRuntime(6642): Caused by: java.lang.NullPointerException
09-10 14:15:37.720: E/AndroidRuntime(6642):     at com.example.issueslist.MainActivity.onCreate(MainActivity.java:100)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.Activity.performCreate(Activity.java:5133)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-10 14:15:37.720: E/AndroidRuntime(6642):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
09-10 14:15:37.720: E/AndroidRuntime(6642):     ... 11 more

Here is some of my code:

package com.example.issueslist;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

TextView severity;

    static final String URL = "URL.xml";
static final String KEY_HELPDESKCALLS = "HELPDESKCALLS";
static final String KEY_DATE = "dateCreated";
static final String KEY_TITLE = "callTitle";
static final String KEY_DESC = "callDescription";
static final String KEY_SEVE = "severityid";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    severity = (TextView) findViewById(R.id.seve);

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);

    NodeList nl = doc.getElementsByTagName(KEY_HELPDESKCALLS);

    for (int i = 0; i < nl.getLength(); i++) {

        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        map.put(KEY_DATE, parser.getValue(e, KEY_DATE).substring(0, 10));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
        map.put(KEY_SEVE, parser.getValue(e, KEY_SEVE).replace("1", "High")
                .replace("2", "Medium").replace("3", "Low"));

        menuItems.add(map);

        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC,
                        KEY_DATE, KEY_SEVE }, new int[] { R.id.name,
                        R.id.desciption, R.id.cost, R.id.seve });

        setListAdapter(adapter);

        ListView list = getListView();

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String title = ((TextView) view.findViewById(R.id.name))
                        .getText().toString();
                String date = ((TextView) view.findViewById(R.id.cost))
                        .getText().toString();
                String description = ((TextView) view
                        .findViewById(R.id.desciption)).getText()
                        .toString();
                String priority = ((TextView) view.findViewById(R.id.seve))
                        .getText().toString();

                Intent in = new Intent(getApplicationContext(),
                        MenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_DATE, date);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_SEVE, priority);
                startActivity(in);

            }
        });

    }

    String value = severity.getText().toString();

    if (value.equals("High")) {

        severity.setTextColor(Color.RED);

    } else if (value.equals("Medium")) {

        severity.setTextColor(Color.YELLOW);

    }
}

}

4

4 回答 4

0
String value = severity.getText().toString();

之后做这个severity = (TextView) findViewById(R.id.seve);

您甚至在 oncreate 之前尝试读取 textview 中的值,在 setContentView 之后您的所有视图都被夸大了。

于 2013-09-10T10:25:15.593 回答
0

问题出在这一行:

String value = severity.getText().toString(); available above onCreate() method

您正在尝试severity访问null.

从那里删除此行并PriorityColour()在开头添加此行。

于 2013-09-10T10:06:29.860 回答
0

你在oncreate中为你的textview做了findviewbyid吗...

于 2013-09-09T16:29:29.877 回答
0

尝试这个。您正在关闭 onclick 侦听器之前的 for 循环。

package com.example.issueslist;

导入 java.util.ArrayList;导入 java.util.HashMap;

导入 org.w3c.dom.Document;导入 org.w3c.dom.Element;导入 org.w3c.dom.NodeList;

导入android.app.ListActivity;导入android.content.Intent;导入android.graphics.Color;导入android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

TextView severity;

static final String URL = "URL.xml";
static final String KEY_HELPDESKCALLS = "HELPDESKCALLS";
static final String KEY_DATE = "dateCreated";
static final String KEY_TITLE = "callTitle";
static final String KEY_DESC = "callDescription";
static final String KEY_SEVE = "severityid";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

severity = (TextView) findViewById(R.id.seve);

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>> ();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);

NodeList nl = doc.getElementsByTagName(KEY_HELPDESKCALLS);

for (int i = 0; i < nl.getLength(); i++) {

    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);

    map.put(KEY_DATE, parser.getValue(e, KEY_DATE).substring(0, 10));
    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
    map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
    map.put(KEY_SEVE, parser.getValue(e, KEY_SEVE).replace("1", "High")
            .replace("2", "Medium").replace("3", "Low"));

    menuItems.add(map);

    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC,
                    KEY_DATE, KEY_SEVE }, new int[] { R.id.name,
                    R.id.desciption, R.id.cost, R.id.seve });

    setListAdapter(adapter);

    ListView list = getListView();

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String title = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String date = ((TextView) view.findViewById(R.id.cost))
                    .getText().toString();
            String description = ((TextView) view
                    .findViewById(R.id.desciption)).getText()
                    .toString();
            String priority = ((TextView) view.findViewById(R.id.seve))
                    .getText().toString();

            Intent in = new Intent(getApplicationContext(),
                    MenuItemActivity.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_DATE, date);
            in.putExtra(KEY_DESC, description);
            in.putExtra(KEY_SEVE, priority);
            startActivity(in);


    });
}


String value = severity.getText().toString();

if (value.equals("High")) {

    severity.setTextColor(Color.RED);

} else if (value.equals("Medium")) {

    severity.setTextColor(Color.YELLOW);

}
}
}
于 2013-09-10T14:41:11.000 回答