EditView Inhalt durch click an Activity übergeben

K

Kopfgut

Neues Mitglied
0
Hallo zusammen,

fange gerade an mich mit Android zu beschäftigen und ich hänge jetzt schon sehr lange an einem Problem mit einem Intent.
Ich habe meinen Code schon so weit wie es geht vereinfacht aber es funktioniert einfach nicht.
Ich hab 2 Activitys und 2 Views. Ich möchte einfach den Text aus der ersten View per Intent an die 2-te View übergeben. Wenn ich es auf dem Emulator ausführe kommt immer die Fehlermeldung The application test ... has stopped unexpectedly

Im debug erscheint:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.kopfgut.apps.test/de.kopfgut.apps.test.ActivityZwei}: java.lang.NullPointerException

Kann mir jemand vielleicht weiterhelfen?

ActivityEins:
Code:
package de.kopfgut.apps.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ActivityEins extends Activity implements OnClickListener  {
    final static String VARIABLE = "eingegeben";  
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Implementieren des OnClickListener
        Button button = (Button)findViewById(R.id.btn_uebergeben);
        button.setOnClickListener(this);
    }
    public void onClick(View v) {
        final EditText dieeingabe = (EditText)findViewById(R.id.edt_eingabe);
        final String eingegeben = dieeingabe.getText().toString();
        
        final Intent intent = new Intent(this, ActivityZwei.class);
        intent.putExtra(VARIABLE, eingegeben);
        startActivity(intent);
    }  
}

ActivityZwei:
Code:
package de.kopfgut.apps.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ActivityZwei extends Activity {
    public String eingegeben;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anzeigen);        
        
        final Bundle extras = getIntent().getExtras();
        eingegeben = extras.getString(ActivityEins.VARIABLE);
        
        textAnzeigen();
    }
    
    public void textAnzeigen()
    {
        final TextView txtBetrag = (TextView)findViewById(R.id.txt_eingabe_ausgabe);
        txtBetrag.setText(String.valueOf(eingegeben));
    }
}

Manifest:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.kopfgut.apps.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityEins"
                  android:label="@string/app_name"
                  android:debuggable="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name=".ActivityZwei"/>
    </application>
    <uses-sdk android:minSdkVersion="6"/>
</manifest>
 
Willkommen im Club.

Es wäre hilfreich, wenn Du mal die Zeile postest, in der die NullpointerException auftritt.
Ist in einer Zeile hinter findViewById?
Dann ist in der layout Datei main.xml die entsprechende id nicht oder anders definiert.
 
Hier sind die Meldungen die der Debug ausgibt:
Für mich ist das Thema noch ziemlich neu und irgendwie werd ich daraus nicht wirklich schlau!

Code:
threadid=3: thread exiting with uncaught exception (group=0x4001b188)
ERROR/AndroidRuntime(207): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(207): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.kopfgut.apps.test/de.kopfgut.apps.test.ActivityZwei}: java.lang.NullPointerException
ERROR/AndroidRuntime(207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
ERROR/AndroidRuntime(207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)
ERROR/AndroidRuntime(207):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
ERROR/AndroidRuntime(207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
ERROR/AndroidRuntime(207):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(207):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(207):     at android.app.ActivityThread.main(ActivityThread.java:4338)
ERROR/AndroidRuntime(207):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(207):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
ERROR/AndroidRuntime(207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
ERROR/AndroidRuntime(207):     at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(207): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(207):     at de.kopfgut.apps.test.ActivityZwei.textAnzeigen(ActivityZwei.java:26)
ERROR/AndroidRuntime(207):     at de.kopfgut.apps.test.ActivityZwei.onCreate(ActivityZwei.java:20)
ERROR/AndroidRuntime(207):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
ERROR/AndroidRuntime(207):     ... 11 more
 
Vielleicht das final vor den Instanziierungen wegnehmen?
 
Das Edittext, welches du mit findViewById holen willst, kommt nicht in der View vor, die du mit setContentView gesetzt hast. :scared:
 
Code:
ERROR/AndroidRuntime(207): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(207):     at de.kopfgut.apps.test.ActivityZwei.textAnzeigen(ActivityZwei.java:26)
Zeile 26 in ActivityZwei:
Code:
        txtBetrag.setText(String.valueOf(eingegeben));
Klarer Fall, txtBeitrag ist null,
weil es keinen View mit der ID txt_eingabe_ausgabe (Zeile 25)
im Layout anzeigen.xml (Zeile 15) gibt.

Wenn Du in einer Activity die Daten einer anderen nutzen willst,
musst Du sie entweder beim Intent als "Extra" mitgeben,
oder (quasi als globale Variable) in dem Application Objekt speichern.
 
Zuletzt bearbeitet:
Oh ja!!! Das hab ich die ganze Zeit übersehen :sleep:

Ich hatte:
Code:
<TextView android:[B]text[/B]="@+id/txt_eingabe_ausgabe"

Es muss natürlich so aussehen:
Code:
<TextView android:[B]id[/B]="@+id/txt_eingabe_ausgabe"

Vielen dank nochmals an alle für die spitzenmäßigen Hinweise.
 

Ähnliche Themen

M
Antworten
8
Aufrufe
952
deek
D
FabianDev
Antworten
5
Aufrufe
547
swa00
swa00
M
  • maksimilian
Antworten
15
Aufrufe
1.694
maksimilian
M
Zurück
Oben Unten