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);
}
}
}