Hallo!
Habe letztens zu Übungszwecken eine App zu schreiben begonnen, habe jedoch ein großes Problem bei dem Widget "EditText"
Mein Programm stürzt immer ab und ich vermute eine NullPointerException.
Hier der Code, der MainActivity.java:
Java: MainActivity.java
package at.hierz.maurice.abschlussuebung;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int Re_gesamt;
int Im_gesamt;
private int calculate_Re(int Re_Z1, int Re_Z2){
return Re_Z1+Re_Z2;
}
private int calculate_Im(int Im_Z1, int Im_Z2){
return Im_Z1 + Im_Z2;
}
private void update_gui(){
TextView txtViewRe = (TextView) findViewById(R.id.txtViewRe);
TextView txtViewIm = (TextView) findViewById(R.id.txtViewIm);
String strReGesamt = Integer.toString(Re_gesamt);
String strImGesamt = Integer.toString(Im_gesamt);
txtViewIm.setText(strImGesamt);
txtViewRe.setText(strReGesamt);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Log.d ("UI", "Berechnung-Button wurde gedrückt");
String strZ1Im = ((EditText)findViewById(R.id.editTextZ1Im)).toString();
String strZ2Im = ((EditText) findViewById(R.id.editTextZ2Im)).toString();
String strZ1Re = ((EditText)findViewById(R.id.editTextZ1Re)).toString();
String strZ2Re = ((EditText)findViewById(R.id.editTextZ2Re)).toString();
int Z1Im = Integer.parseInt(strZ1Im);
int Z2Im = Integer.parseInt(strZ2Im);
int Z1Re = Integer.parseInt(strZ1Re);
int Z2Re = Integer.parseInt(strZ2Re);
Re_gesamt = calculate_Re(Z1Re, Z2Re);
Im_gesamt = calculate_Im(Z1Im, Z2Im);
update_gui();
}
}
Alles anzeigen
Hier meine activity_main.xml
Code
<?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: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="at.hierz.maurice.abschlussuebung.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/z1"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/re"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="37dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Im"
android:id="@+id/textView3"
android:layout_marginEnd="46dp"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="120sp"
android:layout_height="wrap_content"
android:id="@+id/editTextZ1Re"
android:layout_below="@+id/textView2"
android:layout_toEndOf="@+id/textView" />
<EditText
android:layout_width="120sp"
android:layout_height="wrap_content"
android:id="@+id/editTextZ1Im"
android:layout_below="@+id/textView3"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/z2"
android:id="@+id/textView4"
android:layout_below="@+id/editTextZ1Re"
android:layout_alignParentStart="true"
android:layout_marginTop="31dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/re"
android:id="@+id/textView5"
android:layout_below="@+id/textView4"
android:layout_alignStart="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Im"
android:id="@+id/textView6"
android:layout_alignBottom="@+id/textView5"
android:layout_alignStart="@+id/textView3" />
<EditText
android:layout_width="120sp"
android:layout_height="wrap_content"
android:id="@+id/editTextZ2Re"
android:layout_below="@+id/textView5"
android:layout_alignStart="@+id/editTextZ1Re" />
<EditText
android:layout_width="120sp"
android:layout_height="wrap_content"
android:id="@+id/editTextZ2Im"
android:layout_below="@+id/textView6"
android:layout_alignStart="@+id/editTextZ1Im" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/berechnen"
android:id="@+id/buttonCalculate"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/editTextZ2Im"
android:onClick="onClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/seriel"
android:id="@+id/textView7"
android:layout_marginTop="31dp"
android:layout_below="@+id/buttonCalculate"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/re"
android:id="@+id/txtViewRe"
android:layout_below="@+id/textView7"
android:layout_alignStart="@+id/editTextZ2Re" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Im"
android:id="@+id/txtViewIm"
android:layout_alignBottom="@+id/txtViewRe"
android:layout_toEndOf="@+id/buttonCalculate" />
</RelativeLayout>
Alles anzeigen
Hoffe ihr könnte mir helfen!
LG