Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ScrollView没有';t在线程中以编程方式滚动_Java_Android - Fatal编程技术网

Java ScrollView没有';t在线程中以编程方式滚动

Java ScrollView没有';t在线程中以编程方式滚动,java,android,Java,Android,所以我想做的基本上是在客户端应用程序发送命令时滚动滚动视图。handleScrolling()方法在onCreate()中工作,但每当我从线程调用它时,它都不会滚动。我认为从线程调用它才是问题所在 公共类提词器活动扩展活动{ 静态滚动视图mScrollView; TextView提词器TextView; 字符串消息; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContent

所以我想做的基本上是在客户端应用程序发送命令时滚动滚动视图。handleScrolling()方法在onCreate()中工作,但每当我从线程调用它时,它都不会滚动。我认为从线程调用它才是问题所在

公共类提词器活动扩展活动{
静态滚动视图mScrollView;
TextView提词器TextView;
字符串消息;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_提词器);
teleprompterTextView=findViewById(R.id.teleprompter\u text\u view);
提词器textView.setText(MainActivity.contents);
mScrollView=findviewbyd(R.id.scroll);
Thread mThread=新线程(new ServerClass());
mThread.start();
handleScrolling(3,正确);
Log.e(“远程”、“开始混凝土浇筑时的手动碾压”);
}
void handleScrolling(最终整数行,最终布尔值下一步){
mScrollView.post(新的Runnable(){
公开募捐{
int currPosition=mScrollView.getScrollY();
int textSize=(int)teleprompterTextView.getTextSize();
如果(下一个){
int newPos=currPosition+(textSize*numLines);
mScrollView.smoothScrollTo(0,newPos);
}
如果(!下一个){
int newPos=currPosition-(textSize*numLines);
如果(新位置<0){
newPos=0;
}
mScrollView.scrollTo(0,newPos);
}
}
});
}
无效handleMessage(字符串消息){
if(message.contains(“下一步:”){
试一试{
int numLine=Integer.parseInt(message.substring(5));
handleScrolling(numLine,真);
返回;
}捕获(NumberFormatException nFE){
nFE.printStackTrace();
Toast.makeText(TeleprompterActivity.this,“数字格式异常”,Toast.LENGTH_SHORT.show();
}
}
if(message.contains(“BACK:”)){
试一试{
int numLine=Integer.parseInt(message.substring(5));
handleScrolling(数字线,假);
}捕获(NumberFormatException nFE){
nFE.printStackTrace();
Toast.makeText(TeleprompterActivity.this,“数字格式异常”,Toast.LENGTH_SHORT.show();
}
}
}
公共类ServerClass实现可运行{
服务器套接字服务器套接字;
插座;
DataInputStream DataInputStream;
字符串接收数据;
Handler=newhandler();
@凌驾
公开募捐{
试一试{
serverSocket=新的serverSocket(8080);
Log.e(“电话”、“等待客户”);
while(true){
socket=serverSocket.accept();
dataInputStream=新的dataInputStream(socket.getInputStream());
receivedData=dataInputStream.readUTF();
handler.post(新的Runnable(){
@凌驾
公开募捐{
消息=接收数据;
handleMessage(消息);
}
});
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
handleScrolling()方法在线程外部调用时可以完美地工作。是否有参考问题或类似的问题。
谢谢..

不要直接从线程更新UI组件,请调用Activity的runOnUiThread,以便使用如下代码构造:

public class TeleprompterActivity extends Activity {
static ScrollView mScrollView;
TextView teleprompterTextView;
String message;
Activity thisActivity;//<--- used for saving reference to this Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teleprompter);
    teleprompterTextView = findViewById(R.id.teleprompter_text_view);
    teleprompterTextView.setText(MainActivity.contents);
    mScrollView = findViewById(R.id.scroll);

    thisActivity = this;//<---- saving a reference to this activity

    Thread mThread = new Thread(new ServerClass());
    mThread.start();
    handleScrolling(3, true);
    Log.e("TELE", "handleScrolling at start onCrete");
}

void handleScrolling(final int numLines, final boolean next) {

    thisActivity.runOnUiThread(new Runnable()//<--used thisActivity.runOnUiThread here 
    {
        public void run() {
            int currPosition = mScrollView.getScrollY();
            int textSize = (int) teleprompterTextView.getTextSize();
            if (next) {
                int newPos = currPosition + (textSize * numLines);
                mScrollView.smoothScrollTo(0, newPos);
            }
            if (!next) {
                int newPos = currPosition - (textSize * numLines);
                if (newPos < 0) {
                    newPos = 0;
                }
                mScrollView.scrollTo(0, newPos);
            }
        }
    });
}
公共类提词器活动扩展活动{
静态滚动视图mScrollView;
TextView提词器TextView;
字符串消息;

Activity thisActivity;//您应该避免从主线程以外的线程更新UI您的代码正常,您可以在
message=receivedData;
行打印
消息
以查看其值吗?它显示一个错误,该方法无法应用于无效方法我编辑了我的答案,我没有注意到mScrollView.post(),将thisActivity.runOnUiThread()作为参数传递给它是没有意义的!!此外,在这个修改后的答案中,您甚至不需要保存对thisActivity的引用,您可以使用它来代替!!它仍然不滚动。。所有内容都正确显示所有值,但滚动视图仍然不滚动