Reputation: 23
I have been testing a simple calculator app for android but it keeps on crashing when I use button OnClick listener. I tryed using android:OnClick="Add" and it worked correctly but only if I put all the txtA = (EditText) findViewById(R.id.txtA); inside the "Add" subprogram. I really want to use OnClick listener so any help would be appreciated. Here is my code:
package com.example.calculadora;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class Main extends ActionBarActivity {
private EditText txtA;
private EditText txtB;
private TextView lblSuma;
private Button btnsumar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
txtA = (EditText) findViewById(R.id.txtA);
txtB = (EditText) findViewById(R.id.txtB);
lblSuma = (TextView) findViewById(R.id.lblSuma);
btnsumar = (Button) findViewById(R.id.btnSuma);
btnsumar.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//float A = Float.parseFloat(txtA.getText().toString());
//float B = Float.parseFloat(txtB.getText().toString());
//float Suma = A + B;
//lblSuma.setText(""+Suma);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Here is the LogCat:
04-13 10:48:17.966: W/ApplicationPackageManager(26281): getCSCPackageItemText()
04-13 10:48:18.021: D/AndroidRuntime(26281): Shutting down VM
04-13 10:48:18.021: W/dalvikvm(26281): threadid=1: thread exiting with uncaught exception (group=0x4180ac08)
04-13 10:48:18.021: E/AndroidRuntime(26281): FATAL EXCEPTION: main
04-13 10:48:18.021: E/AndroidRuntime(26281): Process: com.example.calculadora, PID: 26281
04-13 10:48:18.021: E/AndroidRuntime(26281): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculadora/com.example.calculadora.Main}: java.lang.NullPointerException
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.access$900(ActivityThread.java:161)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.os.Looper.loop(Looper.java:157)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-13 10:48:18.021: E/AndroidRuntime(26281): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 10:48:18.021: E/AndroidRuntime(26281): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-13 10:48:18.021: E/AndroidRuntime(26281): at dalvik.system.NativeStart.main(Native Method)
04-13 10:48:18.021: E/AndroidRuntime(26281): Caused by: java.lang.NullPointerException
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.example.calculadora.Main.onCreate(Main.java:39)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.Activity.performCreate(Activity.java:5426)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
04-13 10:48:18.021: E/AndroidRuntime(26281): ... 11 more
Upvotes: 2
Views: 8592
Reputation: 24853
Try this..
I guess Button
EditText
TextView
are belongs to fragment_main.xml
so try like below .
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
txtA = (EditText) rootView.findViewById(R.id.txtA);
txtB = (EditText) rootView.findViewById(R.id.txtB);
lblSuma = (TextView) rootView.findViewById(R.id.lblSuma);
btnsumar = (Button) rootView.findViewById(R.id.btnSuma);
btnsumar.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
return rootView;
}
}
Upvotes: 3
Reputation: 182
The problem is with your onclick code. Change it to the following:
btnsumar .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Upvotes: 0