我正在构建一个将XML解析为ListView的应用程序。 ListView显示'问题'以及这些问题的严重程度。
在XML文件' severityID'是1,2或3,但使用' replace()'我在我的代码中将此更改为高,中和低(可能不相关)。
当TextView说' High'我想将setTextColor设置为红色,并且当' Medium'黄色等。
我认为使用if语句会很容易:
if (severity.getText().toString.equalsIgnoreCase("High")) {
severity.setTextColor(Color.RED);
}
然而我的申请人员关闭了。
我也尝试过:
if (value.equals("High")) {
severity.setTextColor(Color.RED);
}
我想我在这里错过了一些明显的东西,但我只有3个月的时间才开始发展,所以请原谅我。
提前致谢。
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
以下是我的一些代码:
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);
}
}
}
答案 0 :(得分:0)
你是否在oncreate中为你的textview做了findviewbyid ...
答案 1 :(得分:0)
问题出在这一行:
String value = severity.getText().toString(); available above onCreate() method
您正在尝试访问severity
null
对象。
从那里删除此行,并在开头的PriorityColour()
中添加此行。
答案 2 :(得分:0)
String value = severity.getText().toString();
在severity = (TextView) findViewById(R.id.seve);
您正试图在onConate之前读取textview中的值,其中所有视图在setContentView之后都会膨胀。
答案 3 :(得分:0)
试试这个。在onclick监听器关闭之前,您正在关闭for循环。
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);
}
}
}