2016-12-25 3 views
-1

Ich möchte vom Benutzer seinen Namen in einem EditText Widget eingeben, dann sein Gewicht in anderen EditText Widget eingeben, und danach die Daten von einem fortfahren Aktivität zu einem anderen.Übergabe Benutzereingabe (EditText) zwischen Aktivitäten, und dann mit der Formel

Gewichtseingabe sollte später in einer Formel enthalten sein, und die Namenseingabe sollte in TextView Widget mit Ergebnis der Formel abgerechnet werden.

Aber wenn ich meine App starte, zeigt mir Android Studio am Ende NullPointerException. Ich nehme an, dass ein Fehler irgendwo zwischen den Aktivitäten liegt. Also, hier ist der Code. Wenn Sie können, pls help me :)

ActivityOne (Name der Aktivität ist InformacijeM) XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_informacije_m" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.alkometar.InformacijeM" 
    android:background="@drawable/activitybackground"> 

    <TextView 
     android:text="Ukucaj svoje ime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="29dp" 
     android:textSize="25dp" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" 
     android:id="@+id/UkucajIme" /> 


    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/strelamanja" 
     android:id="@+id/strelica" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="28dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="" 
     android:ems="10" 
     android:id="@+id/ImeInput" 
     android:layout_marginTop="12dp" 
     android:layout_below="@+id/UkucajIme" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="" 
     android:ems="10" 
     android:id="@+id/MasaInput" 
     android:layout_marginBottom="53dp" 
     android:layout_above="@+id/strelica" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     /> 

    <TextView 
     android:text="Ukucaj svoju masu u kg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/UkucajMasu" 
     android:textSize="25dp" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" 
     android:layout_marginBottom="30dp" 
     android:layout_above="@+id/MasaInput" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

ActivityOne (InformacijeM) Java-Datei:

package com.example.alkometar; 

import android.content.Intent; 
import android.content.res.Resources; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageButton; 

public class InformacijeM extends AppCompatActivity { 

    EditText ImeTxt, MasaTxt ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_informacije_m); 

     // INICIRANJE VREDNOSTI 


     EditText ImeTxt = (EditText)findViewById(R.id.ImeInput); 
     final String str1 = ImeTxt.getText().toString(); 

     EditText MasaTxt = (EditText)findViewById(R.id.MasaInput); 
     final String str2 = MasaTxt.getText().toString(); 

     ImageButton btn = (ImageButton)findViewById(R.id.strelica); 
     // PREBACIVANJE AKTIVITIJA 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent1 = new Intent(InformacijeM.this, RezultatM.class); 
       intent1.putExtra("keyIme", str1); 
       intent1.putExtra("keyMasa", str2); 

       Intent intent = new Intent(InformacijeM.this, StanjeM.class); 
       startActivity(intent); 


      } 
     }); 
    }} 

ActivityTwo (Name der Aktivität XML-Datei ist RezultatM):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_rezultat_m" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.alkometar.RezultatM" 
    android:background="@drawable/activitybackground"> 

    <TextView 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="97dp" 
     android:id="@+id/rezultatTekst" /> 

    <Button 
     android:text="Rezultat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="26dp" 
     android:id="@+id/button" /> 
</RelativeLayout> 

ActivityTwo (RezultatM) Java-Datei:

package com.example.alkometar; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class RezultatM extends AppCompatActivity { 


    TextView rezultat; 


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


     rezultat = (TextView) findViewById(R.id.rezultatTekst); 
     Button button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Bundle extras = getIntent().getExtras(); 
       { 
        String str1 = extras.getString("keyIme"); 
        String str2 = extras.getString("keyMasa"); 


       double masad = Double.valueOf(str2); 
       double nekibroj = 0.8; 
       double result = masad * nekibroj; 

       rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result); 
      } 
     }; 
     }); 
    }} 

strings.xml Datei:

<resources> 
    <string name="app_name">Alkometar</string> 
    <string name="ImeInput">ImeInput</string> 
    <string name="MasaInput">MasaInput</string> 

</resources> 

AndroidManifest XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.alkometar"> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".InformacijeM" /> 
     <activity android:name=".StanjeM" /> 
     <activity android:name=".InformacijeZ" /> 
     <activity android:name=".StanjeZ" /> 
     <activity android:name=".PijemM" /> 
     <activity android:name=".RezultatM"></activity> 
    </application> 

</manifest> 

Und hier ist die Ausnahme in Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.alkometar, PID: 30732 
       java.lang.NullPointerException 
        at com.example.alkometar.RezultatM$1.onClick(RezultatM.java:28) 
        at android.view.View.performClick(View.java:4463) 
        at android.view.View$PerformClick.run(View.java:18770) 
        at android.os.Handler.handleCallback(Handler.java:808) 
        at android.os.Handler.dispatchMessage(Handler.java:103) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:5292) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
        at dalvik.system.NativeStart.main(Native Method) 

Vielen Dank einmal nochmal! :)))

+1

Bei welcher Zeile wird die Ausnahme ausgelöst? Wir können das Problem leicht finden, wenn Sie das erkennen könnten. –

+0

Ich lege dar, dass das Problem darin besteht, die Strings durch Aktivitäten zu übergeben ... :) – Arsenik

Antwort

0

Ändern Sie Ihre Aktivität Zwei Klasse:

public class RezultatM extends AppCompatActivity { 


TextView rezultat; 
String str1,str2; 
double masad; 
public static final String MyPREFERENCES = "MyPrefs"; 
SharedPreferences sharedPreferences; 

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


    rezultat = (TextView) findViewById(R.id.rezultatTekst); 
    Button button = (Button) findViewById(R.id.button); 
    //Intent intent = getIntent(); 
    //str1 = intent.getStringExtra("keyIme"); 
    //str2 = intent.getStringExtra("keyMasa"); 
    //getting values from shared preferences 
    SharedPreferences shared = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    str1=shared.getString("keyIme", ""); 
    str2=shared.getString("keyMasa", ""); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

      if(str2!=null){ 
      masad = Double.parseDouble(str2); 
      } 
      double nekibroj = 0.8; 
      double result = masad * nekibroj; 

      rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result); 
    }; 
    }); 
}} 

Auch in Ihrer Aktivität, die Sie beginnen, falsche Absicht

Intent intent1 = new Intent(InformacijeM.this, RezultatM.class); 
intent1.putExtra("keyIme", str1); 
intent1.putExtra("keyMasa", str2); 

Intent intent = new Intent(InformacijeM.this, StanjeM.class); 
// startActivity(intent); //here you are starting another class..check it 
//change it to 
startActivity(intent1); 

Änderung Ihrer InformacijeM.java Klasse:

public class InformacijeM extends AppCompatActivity { 

    EditText ImeTxt, MasaTxt ; 
    public static final String MyPREFERENCES = "MyPrefs"; 
    SharedPreferences sharedPreferences; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_informacije_m); 
     sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 

     // INICIRANJE VREDNOSTI 


     EditText ImeTxt = (EditText)findViewById(R.id.ImeInput); 
     final String str1 = ImeTxt.getText().toString(); 

     EditText MasaTxt = (EditText)findViewById(R.id.MasaInput); 
     final String str2 = MasaTxt.getText().toString(); 
     save_data(str1,str2); 
     ImageButton btn = (ImageButton)findViewById(R.id.strelica); 
     // PREBACIVANJE AKTIVITIJA 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent1 = new Intent(InformacijeM.this, RezultatM.class); 
       intent1.putExtra("keyIme", str1); 
       intent1.putExtra("keyMasa", str2); 

       Intent intent = new Intent(InformacijeM.this, StanjeM.class); 
       startActivity(intent); 


      } 
     }); 
    } 

    public void save_data(String s1,String s2){ 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString("keyIme", s1); 
     editor.putString("keyMasa", s2); 
     editor.commit(); 
     } 
} 
+0

hat es funktioniert? ... – rafsanahmad007

+0

In ActivityTwo, wenn ich dies bearbeite, Variable masad im Ergebnis ist rot (es sagte, dass Android Studio nicht kann lösche das Symbol 'masad'). Sollte ich andere zwei Variablen (nekibroj und result) zusammen mit masad Variable? Und in ActivityOne: Wenn ich auf die Schaltfläche, ich möchte aus der App, um die zweite Aktivität namens StanjeM gehen, aber auch, ich möchte, dass Benutzer Eingabedaten auf die letzte Aktivität gehen, und mit ihnen möchte ich einige berechnen Formel. (RezultatM ist nicht die nect-Aktivität nach InformacijeM. Die nächste Aktivität ist StanjeM). – Arsenik

+0

Überprüfen Sie die bearbeitete Antwort @Arsenik – rafsanahmad007

Verwandte Themen