[英]Android Studio: Code is done, but app is crashing everytime I try to run
[英]Android Studio, app will not run without crashing
我正在嘗試用我的Java開發的Android Studio開發一個非常簡單的應用程序,這是我的第一個應用程序項目。 我目前沒有編寫很多代碼,但是每次應用程序嘗試運行時,它都會“停止工作”並且永遠不會打開。
我不太肯定我正在尋找Logcat的錯誤部分,但這是我相信Logcat告訴我的內容。
2018-10-10 17:21:03.291 11777-20641/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
這就是我項目的全部代碼(幾乎沒有)
Button rollButton = findViewById(R.id.rollButton);
TextView numberRoll = findViewById(R.id.numberRoll);
SeekBar amountSeek = findViewById(R.id.amountSeek);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Random ran = new Random();
ran.nextInt(amountSeek.getProgress());
numberRoll.setText(ran.toString());
}
});
}
}
如果那是正確的錯誤,那么看來這是模擬器空間不足或其他問題。 但是我不知道如何解決這個問題。
有什么幫助嗎?
定義布局文件后,將以下行移動到onCreate函數
Button rollButton = findViewById(R.id.rollButton);
TextView numberRoll = findViewById(R.id.numberRoll);
SeekBar amountSeek = findViewById(R.id.amountSeek);
您在定義資源文件之前調用資源。 這是定義資源文件的功能。
setContentView(R.layout.activity_main);
您的代碼應為
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button rollButton = findViewById(R.id.rollButton);
TextView numberRoll = findViewById(R.id.numberRoll);
SeekBar amountSeek = findViewById(R.id.amountSeek);
rollButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Random ran = new Random();
ran.nextInt(amountSeek.getProgress());
numberRoll.setText(ran.toString());
}
});
}
或者如果你喜歡
Button rollButton;
TextView numberRoll;
SeekBar amountSeek;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollButton = findViewById(R.id.rollButton);
numberRoll = findViewById(R.id.numberRoll);
amountSeek = findViewById(R.id.amountSeek);
rollButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Random ran = new Random();
ran.nextInt(amountSeek.getProgress());
numberRoll.setText(ran.toString());
}
});
}
並確保ID都已在您的xml文件中設置。
只需在R.id之后加上點即可。 然后等待Android Studio顯示您資源文件中所有ID的下拉列表。 這樣,您可以防止錯別字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.