Android-Anwendung Abstürzen bei der Verwendung von button-OnClick-listener

Habe ich getestet, eine einfache Taschenrechner-app für android, aber es hält abstürzt, wenn ich verwenden Sie die Schaltfläche OnClick-listener. Ich habe versucht mit android:OnClick="Add", und es funktioniert einwandfrei aber nur wenn ich die ganzen txtA = (EditText) findViewById(R. id.txtA); in den "Hinzufügen" - Unterprogramm. Ich möchte wirklich zu verwenden OnClick-listener, so dass jede Hilfe wäre sehr geschätzt. Hier ist mein 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;
        }
    }

}

Hier ist der 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
  • Ist, dass btnSuma Button im activity_main.xml?
  • post fragment_main.xml.
InformationsquelleAutor tidus_david | 2014-04-13
Schreibe einen Kommentar