What is wrong in this program, My eclipse IDE doesn't show any errors....when I execute this simple program the emulator shows force close....Anybody please clarify
import android.app.Activity;
import android.view.View.OnClickListener;
import android.view.View;
import android.os.Bundle;
import android.widget.*;
public class HelloWorld extends A开发者_如何学运维ctivity implements OnClickListener {
/** Called when the activity is first created. */
View Et1,Bt1,TxtDisp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.name_getter);
Bt1=(Button)findViewById(R.id.Btn1);
Et1=(EditText)findViewById(R.id.UserInput);
TxtDisp=(TextView)findViewById(R.id.TextDisp);
Bt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userInput=((EditText) Et1).getText().toString();
((TextView)TxtDisp).setText(userInput);
}
}
Do you know how to enable logcat?
please enable logcat and show us the exact exception it is throwing.
in eclipse, it is: Window>Show View>Other>Android>Logcat
Can you post your main.xml here. Something must have gone wrong there. Check whether you have assigned correct names for each view under android:id
in your layout
My guess is there should be a null pointer thrown at this line
Bt1.setOnClickListener(this);
Can you check whether Bt1 is null? if yes your object has not been initialized properly.
Without an error this is really hard,
- Have you declared your activity in the manifest?
- Does that contentview "name_getter" contain the ids Btn1, UserInput and TextDisp?
- Are those id's indeed respectively a Button, an EditText and a Textview?
Does the error start after clicking the button? Could it be that your getText returns null?
If you really can't get the logcat to work, at least add a bunch of try-catches to your code. If it executes you're getting an error in this file, and not somewhere else. You coudl also try to debug with debugmessages using a Toast.
(something like this, just guessing, you should look it up)
Toast t = Toast.maketext(this, "your error", Toast.LOOKTHISUP);
t.show();
Something similar happened to me once.
Are you sure that
R.id.Btn1 is Button
R.id.UserInput is EditText
R.id.TextDisp is TextView
try to do this:
public void onClick(View V) { switch(v.getId(){ case R.id.Bt1: // do here what you want to do break; } }
精彩评论