Android app => Unfortunately project has stopped

Asked

Viewed 303 times

1

I’m studying Android and want to make a simple parameter pass to another Activity but the application is finished in the emulator and logCat shows a Nullpointerexception, someone can help me?

Class:

public class MainActivity extends Activity {


    EditText num1 = (EditText) findViewById(R.id.txtNumber1);
    EditText num2 = (EditText) findViewById(R.id.txtNumber2);
    String n1= num1.getText().toString();
    int numero1 = Integer.parseInt(n1);
    String n2= num2.getText().toString();
    int numero2 = Integer.parseInt(n1);
    int soma=numero1+numero2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button bt = (Button) findViewById(R.id.bt1);
        final Intent it = new Intent(this, Cadastros.class);
        bt.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                it.putExtra("Total", soma);
                startActivity(it);
            }


        });
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click"
    android:gravity="center"
    android:layout_x="134dp"
    android:layout_y="102dp" />

<EditText
    android:id="@+id/txtNumber1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="56dp"
    android:layout_y="179dp"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtNumber2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="69dp"
    android:layout_y="222dp"
    android:ems="10"
    android:inputType="number" />

</AbsoluteLayout>

Log

06-16 08:09:48.240: D/AndroidRuntime(948): Shutting down VM
06-16 08:09:48.270: W/dalvikvm(948): threadid=1: thread exiting with uncaught exception (group=0x41465730)
06-16 08:09:48.330: E/AndroidRuntime(948): FATAL EXCEPTION: main
06-16 08:09:48.330: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.passagemparam/br.com.passagemparam.MainActivity}: java.lang.NullPointerException
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.os.Looper.loop(Looper.java:137)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-16 08:09:48.330: E/AndroidRuntime(948):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948):     at java.lang.reflect.Method.invoke(Method.java:525)
06-16 08:09:48.330: E/AndroidRuntime(948):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-16 08:09:48.330: E/AndroidRuntime(948):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 08:09:48.330: E/AndroidRuntime(948):     at dalvik.system.NativeStart.main(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948): Caused by: java.lang.NullPointerException
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.Activity.findViewById(Activity.java:1853)
06-16 08:09:48.330: E/AndroidRuntime(948):     at br.com.passagemparam.MainActivity.<init>(MainActivity.java:14)
06-16 08:09:48.330: E/AndroidRuntime(948):     at java.lang.Class.newInstanceImpl(Native Method)
06-16 08:09:48.330: E/AndroidRuntime(948):     at java.lang.Class.newInstance(Class.java:1130)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-16 08:09:48.330: E/AndroidRuntime(948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
06-16 08:09:48.330: E/AndroidRuntime(948):     ... 11 more
  • The one on line 14 in the Mainactivity class.java?

  • Hello, I don’t know how the annotation got there. But the error persists.

  • Row 14 contains the annotation @Override?

  • Yes.... I had, I must have accidentally pasted, I’ve removed it already.

  • moved the lines of Edit text into the onClick method and the application opened, however, by typing the numbers in the fields and clicking the button returns error: Nuberformatexception invalid int ""

  • Now I realize why the error. You must use findViewById() within the onCreate() because if you execute the method before that he won’t find the view because it does not even have layout assigned to Activity, since this is done in the onCreate().

Show 1 more comment

2 answers

0

the result that way and it worked:

insira o código aqui
  AlertDialog.Builder dialogo = new AlertDialog.Builder(Cadastros.this);
            // setando título
            dialogo.setTitle("Resultado");
            // setando mensagem
            dialogo.setMessage("Soma: " + total);
            // setando botão
            dialogo.setNeutralButton("OK", null);
            // chamando o AlertDialog
            dialogo.show();

0

You must use findViewById() within the onCreate() because if you execute the method before that he won’t find the view because it does not even have layout assigned to Activity, since this is done in the onCreate().

Therefore, the right thing would be to use it like this:

public class MainActivity extends Activity {        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         EditText num1 = (EditText) findViewById(R.id.txtNumber1);
         EditText num2 = (EditText) findViewById(R.id.txtNumber2);
         String n1= num1.getText().toString();
         int numero1 = Integer.parseInt(n1);
         String n2= num2.getText().toString();
         int numero2 = Integer.parseInt(n1);
         int soma=numero1+numero2;

         Button bt = (Button) findViewById(R.id.bt1);
         final Intent it = new Intent(this, Cadastros.class);
         bt.setOnClickListener(new Button.OnClickListener() {

              @Override
              public void onClick(View v) {
                it.putExtra("Total", soma);
                startActivity(it);
              }
         });
}
  • Strange friend, that way you specified gave the same error now when I put inside the onClick method works.

  • Strange indeed. rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.