首页 文章

无法在 Headers textview上设置文本

提问于
浏览
0

我无法在 Headers 项上获取setText而无错误地工作 . 我试过打电话

TextView head = (TextView) findViewById(R.id.storyTitle);
         head.setText(st.getString(st.getColumnIndex("titledesc")));

在我的runnable中,在我的filldata()和我的on create中 . 如果我有该代码的第二行它崩溃了 . 如果不是它运行但不会得到我需要的结果 . 我觉得我错过了一些东西,但是不明白为什么这对于 Headers 不适用于其他一切 .

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

2 回答

  • 1

    在你的 private void fillData() 如果你正在比较 if conditions 中的字符串那么,

    应该是 if(main.quickStart == "Cate")if(main.quickStart == "Quick")

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

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

    在onResume()中,你使用你的Runnable对象 . 在你的runnable中,你使用Cursor c设置 Headers title的文本 . 但是光标不会在onResume中初始化,因为你没有调用fillData()那里 . 所以当您的runnable对象尝试访问游标c时,它将抛出异常 .

    所以你需要改变你的onResume(),如:

    @Override
        protected void onResume() {
    
            super.onResume();
            mydbhelper.open();
            fillData();
    
            mHandler.postDelayed(mTask, 10);
        }
    

    尝试使用这个 . 我希望,它会解决你的问题!

相关问题