Android 应用程序在单击按钮后失败并停止

标签 android android-emulator android-widget

我启动了一个小型安卓应用程序来学习。我在屏幕上放置了一个输入文本和按钮。那显示在模拟器上。在我按下按钮(它有一个 onClickListener)之后,应用程序崩溃并在模拟器的屏幕上告诉我:“应用程序......已经意外停止。再试一次”并且我有一个“强制关闭”,当然会关闭应用程序。 我清理并重建了很多次项目。我没有显示任何错误。(使用 eclipse) 这是布局/main.xml:

<ScrollView android:id="@+id/widget37" android:layout_width="247px"
    android:layout_height="282px" android:scrollbars="vertical"
    android:layout_x="38px" android:layout_y="130px">
</ScrollView>
<RadioButton android:id="@+id/compactList"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Compact List" android:layout_x="36px" android:layout_y="78px">
</RadioButton>
<RadioButton android:id="@+id/detailedList"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Detailed List" android:layout_x="176px"
    android:layout_y="78px">
</RadioButton>

这是 Activity 类:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ShowStuffs extends Activity implements View.OnClickListener {

private Button searchbtn;
private EditText input;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    searchbtn = (Button)findViewById(R.id.searchBtn);
    searchbtn.setOnClickListener(this);
}
public void onClick(View view) {
    System.out.println("on click...");
    String searchedText = input.getText().toString();

}
}

我不知道它有什么问题。我们非常欢迎任何建议。谢谢。

最佳答案

在你的layout/main.xml中,你需要声明searchBtn

像这样:

<Button android:id="@+id/searchBtn"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Search Button"/>

更新:我尝试使用以下代码没有错误。

package com.dude.test1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class foo extends Activity implements View.OnClickListener {

        private Button searchbtn;
        private EditText input;

        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            searchbtn = (Button)findViewById(R.id.searchBtn);
            input = (EditText) findViewById(R.id.input);
            searchbtn.setOnClickListener(this);
        }
        public void onClick(View view) {
            //System.out.println("on click...");
Toast.makeText(this, "clicked!", Toast.LENGTH_SHORT).show();
            String searchedText = input.getText().toString();

        }


}

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#FFFFFF">
    <ScrollView android:id="@+id/widget37" android:layout_width="247px"
        android:layout_height="282px" android:scrollbars="vertical"
        android:layout_x="38px" android:layout_y="130px">
    </ScrollView>
    <RadioButton android:id="@+id/compactList"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Compact List" android:layout_x="36px" android:layout_y="78px">
    </RadioButton>
    <RadioButton android:id="@+id/detailedList"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Detailed List" android:layout_x="176px"
        android:layout_y="78px">
    </RadioButton>
    <Button android:id="@+id/searchBtn" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Search Button" />
    <EditText android:id="@+id/input" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

关于Android 应用程序在单击按钮后失败并停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4277497/

相关文章:

android - 再次漏窗

java - 这个资源是什么?为什么 Android 找不到它?

android - 加速(使用 HAXM)android mac 模拟器卡住

android - 为什么用 "end"缩小的单行 TextView 显示框?

java - TextView 中部分左对齐和部分右对齐的文本。为什么这不起作用?

java - Caused by : java. lang.OutOfMemoryError: Failed to allocate a 341712908 byte allocation with 3854436 free bytes and 90MB 直到OOM

Android XML 解析器捕获异常

android-studio - Android emulator - dummy emulator-5554 和 AVD ... 已经在运行

java - 不幸的是应用程序已停止工作

android - 在 fragment 中填充微调器时出错