Android Can';标题文本视图上的t setText

Android Can';标题文本视图上的t setText,android,header,settext,Android,Header,Settext,我无法使标题项上的setText正常工作。我试过打电话 TextView head = (TextView) findViewById(R.id.storyTitle); head.setText(st.getString(st.getColumnIndex("titledesc"))); 在我的runnable中、在我的filldata()中以及在我的创建中。如果我有第二行代码,它就会崩溃。如果没有,它运行,但不会得到我需要的结果。我觉得我遗漏了一些东西,但我不明白为什么

我无法使标题项上的setText正常工作。我试过打电话

 TextView head = (TextView) findViewById(R.id.storyTitle);
         head.setText(st.getString(st.getColumnIndex("titledesc")));
在我的runnable中、在我的filldata()中以及在我的创建中。如果我有第二行代码,它就会崩溃。如果没有,它运行,但不会得到我需要的结果。我觉得我遗漏了一些东西,但我不明白为什么这不适用于标题,而适用于其他所有内容

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

        }
}
经过反复试验,我终于能够让它工作了

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


    }

在您的
private void fillData()
中,如果要比较中的字符串,则

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

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

在onResume()中,您使用您的Runnable对象。在Runnable中,您使用光标c设置标题的文本。但由于您不在onResume中调用fillData(),因此光标不会在onResume中初始化。因此,当您的Runnable对象尝试访问光标c时,它将引发异常

因此,您需要更改onResume(),如下所示:


试试这个。我希望它能解决你的问题

setText(somestring)是正确的。这很好,我们没什么可以告诉你的。如果您有一些意外行为或错误,请尝试谈论其中之一。如果inputpage.java中存在NullPointerException,请提供该文件代码以帮助我们。我将尝试。集合文本首先在filldata中,然后在create中。在runnable只是我在这里发布之前最后一次尝试的地方。不过我会试一试。@maebe:好的。还要检查用于获取列索引的列的拼写,并确保您在正在访问的表中创建了该列。可能有错误。另外,请注意,该列的值在任何情况下都不是空的。将其更改为.equals,但仍然崩溃。一切正常,除非我包括head.setText(st.getString(st.getColumnIndex(“titledesc”));我认为问题在于cursor st。只需删除这两行,TextView head=(TextView)findViewById(R.id.storytile);head.setText(st.getString(st.getColumnIndex(“titledesc”));并且只输出System.out.println(st.getString(st.getColumnIndex(“titledesc”));如果这也会导致异常,则只需System.out.println(st.getCount());并检查光标的大小。。简单:-)我认为自从st.moveToFirst()以来,光标出现了问题;正在给客户带来问题。Bot那些system.out.println导致它崩溃。似乎您无法在mTask中获取光标数据。它仍然为空。我已将光标更改为st1和st2。接下来,我将System.out.println(st.getCount())放进去;在filldata中,而不是在创建时,它不会崩溃,但不确定它应该在哪里发布结果。
Cursor st = mydbhelper.getQuickStory();
to 
st = mydbhelper.getStory();
if(main.quickStart.equals("Cate"))
if(main.quickStart.equals("Quick"))
@Override
    protected void onResume() {

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

        mHandler.postDelayed(mTask, 10);
    }