Android application fails and stops after clicking a button
i started a small android application to learn. I placed an input text and button on the screen. That shows on the emulator. After i press the button(which has an onClickListener) the application cracks and tells me on the screen of the emulator : "The application... has stopped unexpectably.Try again" and i have a "Force close", which of course closes the application. I cleaned and rebuild the project a lot of times. i have no errors shown.(using eclipse) this is the layout/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>
this is the activity class:
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();
}
}
I cannot figure out what is wrong with it. Any suggestions are more than welcomed.Thank you .
In your layout/main.xml, you need to declare the searchBtn
Something like this:
<Button android:id="@+id/searchBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search Button"/>
Update: I tried with following code with no error.
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 file:
<?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>
Looks like a NullPointerException.
String searchedText = input.getText().toString();
Where the input gets its value?
add this to both buttons: android:onClick ="onClick"
精彩评论