gpt4 book ai didi

android - 为什么我的Android应用程序崩溃?

转载 作者:行者123 更新时间:2023-12-03 15:48:45 26 4
gpt4 key购买 nike

当我调试我的应用程序时,它崩溃了。
我收到消息“很遗憾,XXX已停止错误”。
该应用程序从SD读取文件。

这是Logcat消息:

01-15 22:08:49.878  20190-20190/com.example.ReadFile I/System.out﹕ debugger has settled (1378)
01-15 22:08:49.962 20190-20209/com.example.ReadFile D/OpenGLRenderer﹕ Render dirty regions requested: true
01-15 22:08:49.966 20190-20190/com.example.ReadFile D/Atlas﹕ Validating map...
01-15 22:08:50.046 20190-20209/com.example.ReadFile I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-15 22:08:50.068 20190-20209/com.example.ReadFile D/OpenGLRenderer﹕ Enabling debug mode 0
01-15 22:08:50.070 20190-20209/com.example.ReadFile D/mali_winsys﹕ new_window_surface returns 0x3000
01-15 22:08:58.528 20190-20190/com.example.ReadFile I/Choreographer﹕ Skipped 129 frames! The application may be doing too much work on its main thread.
01-15 22:09:11.207 20190-20198/com.example.ReadFile A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xdead1234 in tid 20198 (JDWP)

这是代码:

这是 Activity :
public class MyActivity extends Activity {

private UdpClient mUdpClient;
private TextView txtTest;
private Button btnDisconnect;
private Button btnConnect;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txtTest = (TextView)findViewById(R.id.txtTest);
btnConnect = (Button)findViewById(R.id.btnConnect);

btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectToServer();
}
});
}

private void ConnectToServer() {
new ConnectTask().execute("");
}
public class ConnectTask extends AsyncTask<String, Bitmap, UdpClient> {
@Override
protected UdpClient doInBackground(String... message) {

mUdpClient = new UdpClient(getApplicationContext());
mUdpClient.run();
return null;
}

@Override
protected void onProgressUpdate(Bitmap... bitmap){
super.onProgressUpdate(bitmap);
}
}
}

这是读取文件的代码:
public class UdpClient {
public static Context ctx;


public UdpClient(Context context) {
ctx = context;
}

public void run()
{
int valueIndex = R.raw.image_nearest;
int imageXYs = R.raw.image_xy;

String[] strNearestValue = this.ReadValueFile(valueIndex);
String[] imageXY = ReadValueFile(imageXYs);

int []nearestValue = new int[strNearestValue.length];

int numOfPixel = imageXY.length;
int []pixels = new int[numOfPixel];

for(int i=0;i<imageXY.length;i++)
{
nearestValue[i] = Integer.parseInt(strNearestValue[i]);
pixels[i]=GetXY(imageXY[i]);
}
}

public static int GetXY(String xyRaw) {
String[] xy = xyRaw.split(",");
short xPixel = Short.parseShort(xy[0]);
short yPixel = Short.parseShort(xy[1]);
int pixelIndex = 800 * (yPixel - 1) + xPixel - 1;
return pixelIndex;
}


private String[] ReadValueFile(int fileResourceID) {
List<String> list = new ArrayList<String>();
try(InputStream inputStream = ctx.getResources().openRawResource(fileResourceID);){
try(BufferedReader br= new BufferedReader(new InputStreamReader(inputStream)))
{
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list.toArray(new String[list.size()]);
}
}

这是堆栈跟踪:
01-15 22:02:00.884  19747-19847/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.ReadFile, PID: 19747
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ReadFile.UdpClient.run()' on a null object reference
at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:55)
at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:51)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
01-15 22:02:37.108 19870-19901/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.ReadFile, PID: 19870
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ReadFile.UdpClient.run()' on a null object reference
at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:55)
at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:51)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
01-15 22:08:16.826 399-442/? E/InputDispatcher﹕ channel '2f43cf57 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 22:35:42.096 399-442/? E/InputDispatcher﹕ channel '23d7860 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 22:58:22.366 399-442/? E/InputDispatcher﹕ channel '14d3e113 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 23:02:44.483 21264-21272/com.example.ReadFile A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xdead1234 in tid 21272 (JDWP)
01-15 23:02:45.046 399-442/? E/InputDispatcher﹕ channel '16edc311 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

一旦我读取文件,程序就会崩溃。

最佳答案

在我看来,主CPU工作过度,换句话说,这是占用大量资源的,例如,当文件停止响应并在Windows中崩溃时,这基本上是同一件事,请尝试对其进行优化或扩展线程。

关于android - 为什么我的Android应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27972363/

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