Reputation: 311
I am just trying to initiliazing by variables by Buttons and variables. But when i try to initialize the if I set my OnClickListeners for the buttons the applications gets stopped unexpectedly. If i comment the lines of setting the OnClickListener the application is working fine. So I am sure that the problem is definitely with the OnClickListener.I have also attached my activity in the AndroidManifest.XML The class name is Data.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements OnClickListener {
Button start, startFor;
EditText sendET;
TextView gotAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initialise();
start.setOnClickListener(this);
**startFor.setOnClickListener(this);
setContentView(R.layout.get);**
}
private void initialise() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sen
dET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case (R.id.bSA):
break;
case (R.id.bSAFR):
break;
}
}
}
and the corresponding Manifest added activity is
<activity
android:name=".Data"
android:label="@string/app_name" >
</activity>
Whats the error in setting the OnClickListener... is their something wrong that I am doing ?
Upvotes: 0
Views: 2672
Reputation: 6086
Espiandev is right, setContentView(R.id.mylayoutid)
must come right after the super call.
In addition, It might be easier for you to do this in your layout xml:
<Button android:id="@+id/bSA"
...
android:onClick="onClick"/>
<Button android:id="@+id/bSAFR"
...
android:onClick="onClick"/>
You don't even have to implement View.OnClickListener this way, or call setOnClickListener - when the layout XML is inflated, the click listeners will be assigned automatically.
Upvotes: 0
Reputation: 35651
You are setting your content view after your initialise method. You need to do it before you attempt to access your UI components.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialise();
start.setOnClickListener(this);
}
Upvotes: 1
Reputation: 8828
You haven't specified a view for your activity. Just after you call super.onCreate(..)
, call setContentView(R.id.mylayoutid)
, where mylayoutid is the name of the xml layout which contains the buttons.
Upvotes: 4