1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

Aplicação Android => Unfortunately projeto has stopped

Olá, estou estudando Android e quero fazer uma simples passagem de parâmetro para outra Activity mas a aplicação é finalizada no emulador e o logCat mostra um Null pointer Exception, alguém pode me ajudar? Classe

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

Cadastro XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fala Fera"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/mostraTotal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

String XML

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Passagem param</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

</resources>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.passagemparam"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Cadastros">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

LogCAt

06-16 13:06:43.040: E/AndroidRuntime(859): FATAL EXCEPTION: main
06-16 13:06:43.040: E/AndroidRuntime(859): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.passagemparam
/br.com.passagemparam.Cadastros}: android.content.res.Resources$NotFoundException: String resource ID #0x8
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.os.Looper.loop(Looper.java:137)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-16 13:06:43.040: E/AndroidRuntime(859):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 13:06:43.040: E/AndroidRuntime(859):     at java.lang.reflect.Method.invoke(Method.java:525)
06-16 13:06:43.040: E/AndroidRuntime(859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-16 13:06:43.040: E/AndroidRuntime(859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 13:06:43.040: E/AndroidRuntime(859):     at dalvik.system.NativeStart.main(Native Method)
06-16 13:06:43.040: E/AndroidRuntime(859): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x8
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.content.res.Resources.getText(Resources.java:239)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.widget.TextView.setText(TextView.java:3844)
06-16 13:06:43.040: E/AndroidRuntime(859):     at br.com.passagemparam.Cadastros.onCreate(Cadastros.java:23)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.Activity.performCreate(Activity.java:5133)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 13:06:43.040: E/AndroidRuntime(859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-16 13:06:43.040: E/AndroidRuntime(859):     ... 11 more
  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

3 respostas

Não é a resposta que estava procurando? Procure outras perguntas com as tags null exception ou faça a sua própria pergunta.