将TextView中的文本转换为字符串错误

时间:2015-07-04 19:36:48

标签: java android android-studio

更新:我用你给我的代码替换了我的代码,但我遇到了运行时错误。澄清;我有两个TextView,每个都有自己的Id,但里面没有文字。我还有两个声明的字符串(每个TextView一个),每个字符串都有我想要的内容。代码接受这些字符串并将它们放入我的TextViews中,然后按照我设置速度的速率一次打印一个字母的字符串。

这是我目前的MainActivity.java类:

cell.imgView.userInteractionEnabled = YES;

更新:这是我的LetterDisplay类:

package com.example.micor.projectsero2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.micor.projectsero2.util.LetterDisplay;


public class MainActivity extends Activity {
   public void onCreate(Bundle savedInstanceState){
       // ...
       // you can do this in every method not only in onCreate e.g onStart onResume etc.
       // now start LetterDisplay thread
       TextView textView = (TextView) findViewById(R.id.loadView); // if you have another id of textView replace R.id.textView to your id
       String text = getString(R.string.loadText);
       int speed = 30; // you can change it to another value

       LetterDisplay letterDisplay = new LetterDisplay(this, textView, text, speed);
       letterDisplay.start(); // now LetterDisplay thread is running
       // EDIT: and start next LetterDisplay thread for second textView
       String secondText = getString(R.string.prelogText);
       TextView secondTextView = (TextView) findViewById(R.id.prelogView); // replace R.id.second_text_view with your  id of second textView
       letterDisplay = new LetterDisplay(this, secondTextView, secondText, speed);
       letterDisplay.start();
       // and now two threads is running and updates textViews

   }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

运行Logcat:

public class LetterDisplay extends Thread {

private Activity mActivity;
private TextView textView;
private String text;
private int charCount;
private int currentLetterCount = 0;
private int speed; // How fast should it type?
private boolean isRunning = true;



public LetterDisplay(Activity activity, TextView textView, String text, int  speed){
    mActivity = activity;
    this.textView = textView;
    this.text = text;
    this.speed = speed;
}
public void run(){
    while(currentLetterCount < charCount && isRunning){
        String currentText = textView.getText().toString();
        char currentLetter = text.charAt(currentLetterCount++);
        final String textToUpdate = currentText + currentLetter;
        mActivity.runOnUiThread(new Runnable(){
            public void run(){
                textView.setText(textToUpdate);
            }
        });
        try
        {
            Thread.sleep(speed);
        } catch(InterruptedException e){}
    }
    isRunning = false;
}
public void stopThread(){
    isRunning = false;
}
public boolean isRunning(){
    return isRunning;
}
}

07-05 12:40:08.348 3308-3329 /? D / KeyguardUpdateMonitor:isSimPinSecure()返回false 07-05 12:40:08.348 1079-3332 /? W / ActivityManager:强制完成活动1 com.example.micor.projectsero2 / .MainActivity 07-05 12:40:08.358 3308-4541 /? D / KeyguardUpdateMonitor:isSimPinSecure()返回false

2 个答案:

答案 0 :(得分:3)

您要获取的类型不是String类型,因此您应该添加“toString”方法。另外,“getText”方法不适用于参数,因此它应该是:

String currentText = loadView.getText().toString();

答案 1 :(得分:2)

试试这个 String currentText = loadView.getText().toString();

方法getText()不接受任何参数并返回CharSequence。 CharSequence不能分配给String,你必须调用toString()方法。

编辑:FIXED LetterDisplay类:

public LetterDisplay(Activity activity, TextView textView, String text, int  speed){
    mActivity = activity;
    if(mActivity == null){
        throw new IllegalArgumentException("activity cannot be null in LetterDisplay");
    }
    this.textView = textView;
    if(textView == null){
        throw new IllegalArgumentException("textView cannot be null in LetterDisplay");
    }
    this.text = text;
    if(text == null){
        throw new IllegalArgumentException("text cannot be null in LetterDisplay");
    }
    this.speed = speed;
    if(speed <= 0){
        throw new IllegalArgumentException("speed cannot be lower or equals than 0");
    }
    this.charCount = text.length();
    if(charCount == 0){
        // no error but i print the warning into logcat
        Log.w("LetterDisplay", "The text in the LetterDisplay is empty and nothing will be executed");
    }
}
public void run(){
    if(charCount > 0)
        textView.setText(""); // Clear the text if any exists
    while(currentLetterCount < charCount && isRunning){
        final String textToUpdate = text.substring(0, ++currentLetterCount);
        mActivity.runOnUiThread(new Runnable(){
            public void run(){
                textView.setText(textToUpdate);
            }
        });
        try
        {
            Thread.sleep(speed);
        } catch(InterruptedException e){}
    }
    isRunning = false;
}
public void stopThread(){
    // you can call it in onPause or onStop or in onDestroy method to stop the thread and avoid energy consumption
    isRunning = false;
}
public boolean isRunning(){
    // if you want to check if thread is running
    return isRunning;
}
}

现在在您的活动中:

public class MyActivity extends Activity {
       public void onCreate(Bundle savedInstanceState){
           // ...
           // you can do this in every method not only in onCreate e.g onStart onResume etc.
           // now start LetterDisplay thread
           TextView textView = (TextView) findViewById(R.id.textView); // if you have another id of textView replace R.id.textView to your id

           String text = "Text to show. You can change it";
           int speed = 100; // you can change it to another value

           LetterDisplay letterDisplay = new LetterDisplay(this, textView, text, speed);
           letterDisplay.start(); // now LetterDisplay thread is running
           // EDIT: and start next LetterDisplay thread for second textView
           String secondText = "Second text to show";
           TextView secondTextView = (TextView) findViewById(R.id.second_text_view); // replace R.id.second_text_view with your  id of second textView
           letterDisplay = new LetterDisplay(this, secondTextView, secondText, speed);
           letterDisplay.start();
           // and now two threads is running and updates textViews

       }

}