gpt4 book ai didi

java - 在 Android Studio 上测试时尝试读取文件时,应用程序会关闭

转载 作者:行者123 更新时间:2023-12-01 16:38:21 24 4
gpt4 key购买 nike

所以我一直在开发一个应用程序,它可以告诉你随机谈论的话题。我尝试读入文件并显示带有文件上随机主题的文本。我执行此操作的代码在这里:

package com.example.nosir;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import android.util.Log;
import java.util.Random;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final String TAG = MainActivity.class.getSimpleName();
public static final String mPath = "example.txt";
private QuoteBank mQuoteBank;
private List<String> mLines;
Random random = new Random();

TextView text1;
String theTopic;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(this);
text1 = (TextView) findViewById(R.id.text1);

mQuoteBank = new QuoteBank(this);
mLines = mQuoteBank.readLine(mPath);
for (String string : mLines)
Log.d(TAG, string);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
text1.setText(mLines.get(random.nextInt(mLines.size())));
break;
default:

break;
}
}


}
package com.example.nosir;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;

public class QuoteBank {

private Context mContext;

public QuoteBank(Context context) {
this.mContext = context;
}

public List<String> readLine(String path) {
List<String> mLines = new ArrayList<>();

AssetManager am = mContext.getAssets();

try {
InputStream is = am.open(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;

while ((line = reader.readLine()) != null)
mLines.add(line);
} catch (IOException e) {
e.printStackTrace();
}

return mLines;
}
}

但是当我按下按钮显示随机文本时,我的应用程序只是关闭并且不执行任何操作。我正在 OnePlus6 上进行测试。我实在不知道出了什么问题。我希望有人能帮助我。提前致谢。

最佳答案

感谢您的快速回复。我找到了一种自己完成的方法,它比旧代码更短、更好。这是代码,如果有人感兴趣的话:


public class MainActivity extends AppCompatActivity {

Button b_read;
TextView tv_text;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b_read = (Button) findViewById(R.id.b_read);

tv_text = (TextView) findViewById(R.id.tv_text);

b_read.setOnClickListener(new View.OnClickListener(){

public void onClick(View view) {
String text = "";
String words[] = null;
int x = (int) (Math.random()*1556 + 1);
try{
InputStream is = getAssets().open("Themen.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
Pattern pattern = Pattern.compile("\n");
words = pattern.split(text);
} catch (IOException ex){
ex.printStackTrace();
}
tv_text.setText(words[x]);
}

});


}


}

关于java - 在 Android Studio 上测试时尝试读取文件时,应用程序会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61912793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com