java - Android Studio,应用程序将无法运行而不崩溃

标签 java android android-studio crash

我正在尝试在 Android Studio 中为我的第一个应用程序项目制作一个用 Java 编写的非常简单的应用程序。我目前没有编写大量代码,但每次应用程序尝试运行时,它都会“停止工作”并且永远不会打开。

我不确定我是否在 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 的下拉列表。这样就可以防止拼写错误。

关于java - Android Studio,应用程序将无法运行而不崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749724/

相关文章:

java - Spring roo 的元数据定义方法

java - RESTeasy 3.0.16 缺少一些东西 (RESTEASY003210)

java - 如何在 Android Studio 中以编程方式定位 ImageView

android - @ViewDebug.ExportedProperty 到底是什么?

android - 良好的 Android UI 设计模式引用

java - 我在 Android 版本 4 手机上遇到的崩溃问题

android - Android Studio 更新后 Gradle 项目刷新失败

java - 使用匹配项来匹配整个单词

java - 使用 Bing 和 Java 进行地理定位

android - Fragment 内的 CordovaWebView 返回 NullPointerException