簡體   English   中英

無法在標題TextView上設置Text

[英]Can't setText on header textview

我無法讓我的setText放在標題項目上而沒有錯誤。 我試過打電話

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

如果要比較if條件中的字符串, private void fillData()中,

if(main.quickStart == "Cate")if(main.quickStart == "Quick")應該是,

if(main.quickStart.equals("Cate"))

if(main.quickStart.equals("Quick"))

在onResume()中,使用Runnable對象。在runnable中,使用Cursor c設置標題標題的文本。但是不會在onResume中初始化游標,因為您不在那里調用fillData()。當您的可運行對象嘗試訪問游標c時,它將引發異常。

因此,您需要像這樣更改onResume():

@Override
    protected void onResume() {

        super.onResume();
        mydbhelper.open();
        fillData();

        mHandler.postDelayed(mTask, 10);
    }

嘗試使用它。希望能解決您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM