我無法使標題項目上的setText無錯地工作。我試過打電話無法在標題textview上設置文本
TextView head = (TextView) findViewById(R.id.storyTitle);
head.setText(st.getString(st.getColumnIndex("titledesc")));
在我的runnable中,在我的filldata()和我的create中。如果我有第二行代碼,它會崩潰。如果沒有,它會運行,但不會得到我需要的結果。我覺得我錯過了一些東西,但不明白爲什麼這對於其他所有東西不起作用。
public class output extends ListActivity {
private PopupWindow pw;
private dbadapter mydbhelper;
private int getCount = inputpage.editCount;
private Map<Integer, String> values = inputpage.inputValues;
private Cursor st;
@Override
public void onCreate(Bundle savedInstantState){
super.onCreate(savedInstantState);
setContentView(R.layout.outview);
mydbhelper = new dbadapter(this);
mydbhelper.open();
fillData();
}
//Lots of long if statements to insure all text changed has the new color, for statement only colors last item in loop
private final Runnable mTask = new Runnable(){
public void run(){
TextView head = (TextView) findViewById(R.id.storyTitle);
head.setText(st.getString(st.getColumnIndex("titledesc")));
if (getCount == 1){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
textView.setText(modifitedText);
}
if (getCount == 2){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
textView.setText(modifitedText1);
}
if (getCount == 3){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
textView.setText(modifitedText2);
}
if (getCount == 4){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
textView.setText(modifitedText3);
}
if (getCount == 5){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
textView.setText(modifitedText4);
}
if (getCount == 6){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
CharSequence modifitedText5 = Replacer.replace(modifitedText4,
"5", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(5) +"</font>"));
textView.setText(modifitedText5);
}
if (getCount == 7){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
CharSequence modifitedText5 = Replacer.replace(modifitedText4,
"5", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(5) +"</font>"));
CharSequence modifitedText6 = Replacer.replace(modifitedText5,
"6", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(6) +"</font>"));
textView.setText(modifitedText6);
}
if (getCount == 8){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
CharSequence modifitedText5 = Replacer.replace(modifitedText4,
"5", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(5) +"</font>"));
CharSequence modifitedText6 = Replacer.replace(modifitedText5,
"6", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(6) +"</font>"));
CharSequence modifitedText7 = Replacer.replace(modifitedText6,
"7", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(7) +"</font>"));
textView.setText(modifitedText7);
}
if (getCount == 9){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
CharSequence modifitedText5 = Replacer.replace(modifitedText4,
"5", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(5) +"</font>"));
CharSequence modifitedText6 = Replacer.replace(modifitedText5,
"6", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(6) +"</font>"));
CharSequence modifitedText7 = Replacer.replace(modifitedText6,
"7", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(7) +"</font>"));
CharSequence modifitedText8 = Replacer.replace(modifitedText7,
"8", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(8) +"</font>"));
textView.setText(modifitedText8);
}
if (getCount == 10){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"0", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(0) +"</font>"));
CharSequence modifitedText1 = Replacer.replace(modifitedText,
"1", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(1) +"</font>"));
CharSequence modifitedText2 = Replacer.replace(modifitedText1,
"2", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(2) +"</font>"));
CharSequence modifitedText3 = Replacer.replace(modifitedText2,
"3", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(3) +"</font>"));
CharSequence modifitedText4 = Replacer.replace(modifitedText3,
"4", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(4) +"</font>"));
CharSequence modifitedText5 = Replacer.replace(modifitedText4,
"5", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(5) +"</font>"));
CharSequence modifitedText6 = Replacer.replace(modifitedText5,
"6", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(6) +"</font>"));
CharSequence modifitedText7 = Replacer.replace(modifitedText6,
"7", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(7) +"</font>"));
CharSequence modifitedText8 = Replacer.replace(modifitedText7,
"8", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(8) +"</font>"));
CharSequence modifitedText9 = Replacer.replace(modifitedText8,
"9", Html.fromHtml("<font color=\"#FFA500\">"+ values.get(9) +"</font>"));
textView.setText(modifitedText9);
}
};};
private final Handler mHandler = new Handler();
//fill listview with data if statement to get quickstart button to work
private void fillData() {
if(main.quickStart == "Cate"){
Cursor st = mydbhelper.getStory();
startManagingCursor(st);
String[] from = new String[] {dbadapter.KEY_TITLESTORY};
int[] to = new int[] {R.id.outputText};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.out_row, st, from, to);
ListView list = getListView();
View footer = getLayoutInflater().inflate(R.layout.outputbutton, list, false);
View header = getLayoutInflater().inflate(R.layout.outheader, list, false);
list.addHeaderView(header);
list.addFooterView(footer);
setListAdapter(adapter);}
if(main.quickStart == "Quick"){
Cursor st = mydbhelper.getQuickStory();
startManagingCursor(st);
String[] from = new String[] {dbadapter.KEY_TITLESTORY};
int[] to = new int[] {R.id.outputText};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.out_row, st, from, to);
ListView list = getListView();
View footer = getLayoutInflater().inflate(R.layout.outputbutton, list, false);
View header = getLayoutInflater().inflate(R.layout.outheader, list, false);
list.addHeaderView(header);
list.addFooterView(footer);
setListAdapter(adapter);}
}
public void onClickMenu(View footer){
final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
editClickSound.start();
Intent intent = new Intent(this, main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@Override
protected void onResume() {
mydbhelper.open();
mHandler.postDelayed(mTask, 10);
super.onResume();
}
@Override
protected void onPause() {
mydbhelper.close();
super.onPause();
}
//Menu Items
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
showHelp();
return true;
}
return false;
}
private void showHelp() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pw = new PopupWindow(
inflater.inflate(R.layout.help, null, false),
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
true);
pw.showAtLocation(this.findViewById(R.id.outputText), Gravity.CENTER, 0, 0);
}
public void onClickHelp(View helper){pw.dismiss();
}
}
登錄貓
02-28 01:20:09.750: D/dalvikvm(224): GC freed 519 objects/47136 bytes in 104ms
02-28 01:20:10.360: W/MediaPlayer(224): info/warning (1, 44)
02-28 01:20:10.440: V/DB Exists(224): db exists
02-28 01:20:10.730: I/MediaPlayer(224): Info (1,44)
02-28 01:20:13.260: W/MediaPlayer(224): info/warning (1, 44)
02-28 01:20:13.330: I/MediaPlayer(224): Info (1,44)
02-28 01:20:14.600: W/KeyCharacterMap(224): No keyboard for id 0
02-28 01:20:14.610: W/KeyCharacterMap(224): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-28 01:20:18.851: W/MediaPlayer(224): info/warning (1, 44)
02-28 01:20:18.910: I/MediaPlayer(224): Info (1,44)
02-28 01:20:19.220: D/AndroidRuntime(224): Shutting down VM
02-28 01:20:19.220: W/dalvikvm(224): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-28 01:20:19.220: E/AndroidRuntime(224): Uncaught handler: thread main exiting due to uncaught exception
02-28 01:20:19.240: E/AndroidRuntime(224): java.lang.NullPointerException
02-28 01:20:19.240: E/AndroidRuntime(224): at wanted.pro.madlibs.output$1.run(output.java:45)
02-28 01:20:19.240: E/AndroidRuntime(224): at android.os.Handler.handleCallback(Handler.java:587)
02-28 01:20:19.240: E/AndroidRuntime(224): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 01:20:19.240: E/AndroidRuntime(224): at android.os.Looper.loop(Looper.java:123)
02-28 01:20:19.240: E/AndroidRuntime(224): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-28 01:20:19.240: E/AndroidRuntime(224): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 01:20:19.240: E/AndroidRuntime(224): at java.lang.reflect.Method.invoke(Method.java:521)
02-28 01:20:19.240: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-28 01:20:19.240: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-28 01:20:19.240: E/AndroidRuntime(224): at dalvik.system.NativeStart.main(Native Method)
02-28 01:20:19.260: I/dalvikvm(224): threadid=7: reacting to signal 3
02-28 01:20:19.300: I/dalvikvm(224): Wrote stack trace to '/data/anr/traces.txt'
02-28 01:20:21.020: I/Process(224): Sending signal. PID: 224 SIG: 9
02-28 01:20:21.980: D/AndroidRuntime(242): Shutting down VM
02-28 01:20:21.980: W/dalvikvm(242): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-28 01:20:21.980: E/AndroidRuntime(242): Uncaught handler: thread main exiting due to uncaught exception
02-28 01:20:22.000: E/AndroidRuntime(242): java.lang.RuntimeException: Unable to start activity ComponentInfo{wanted.pro.madlibs/wanted.pro.madlibs.inputpage}: java.lang.NullPointerException
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.os.Looper.loop(Looper.java:123)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-28 01:20:22.000: E/AndroidRuntime(242): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 01:20:22.000: E/AndroidRuntime(242): at java.lang.reflect.Method.invoke(Method.java:521)
02-28 01:20:22.000: E/AndroidRuntime(242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-28 01:20:22.000: E/AndroidRuntime(242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-28 01:20:22.000: E/AndroidRuntime(242): at dalvik.system.NativeStart.main(Native Method)
02-28 01:20:22.000: E/AndroidRuntime(242): Caused by: java.lang.NullPointerException
02-28 01:20:22.000: E/AndroidRuntime(242): at wanted.pro.madlibs.inputpage.onCreate(inputpage.java:51)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 01:20:22.000: E/AndroidRuntime(242): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-28 01:20:22.000: E/AndroidRuntime(242): ... 11 more
02-28 01:20:22.020: I/dalvikvm(242): threadid=7: reacting to signal 3
02-28 01:20:22.070: I/dalvikvm(242): Wrote stack trace to '/data/anr/traces.txt'
02-28 01:20:24.530: D/dalvikvm(250): GC freed 563 objects/48816 bytes in 78ms
02-28 01:20:24.842: W/MediaPlayer(250): info/warning (1, 44)
02-28 01:20:24.940: V/DB Exists(250): db exists
02-28 01:20:25.022: I/MediaPlayer(250): Info (1,44)
大量的試驗和錯誤後,嗯,我能得到它的工作。
public class output extends ListActivity {
private PopupWindow pw;
private dbadapter mydbhelper;
private int getCount = inputpage.editCount;
private Map<Integer, String> values = inputpage.inputValues;
public Cursor st;
@Override
public void onCreate(Bundle savedInstantState){
super.onCreate(savedInstantState);
setContentView(R.layout.outview);
mydbhelper = new dbadapter(this);
mydbhelper.open();
fillData();
TextView head = (TextView) findViewById(R.id.storyTitle);
st.moveToFirst();
head.setText(st.getString(st.getColumnIndex(dbadapter.KEY_TITLEDESC)));
}
認爲關鍵是要改變,然後在正確的地點/順序調用它。 TY的所有幫助。
Cursor st = mydbhelper.getQuickStory();
to
st = mydbhelper.getStory();
請提供您的日誌貓 – Nitin 2012-02-28 06:16:16
atextview.setText(somestring)是正確的。這工作得很好,我們沒有什麼可以告訴你的。如果您有一些意外的行爲或錯誤,請嘗試談論其中之一。 – 2012-02-28 06:22:52
您在inputpage.java中有一個NullPointerException,請提供該文件代碼以幫助您。 – 2012-02-28 06:36:41