Hallo,
ich bin etwas frisch an dem Thema Android drann und hab mir gleich mal n paar Fehlermeldungen eingesammel die ich nicht verstehe.
Ich will das die Zahl aus dem Eingabefeld in dem Betrag in dem TextViwer hinzufügt und diesen ablegt.
Wäre super wenn ihr mir helfen könntet =D
Dies ist mein Code:
Java
package me.com.example.sparrechner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SparRechner extends Activity {
private static final String TAG = SparRechner.class.getSimpleName();
private static final String FILENAME = TAG + ".txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spar_rechner);
EditText Betrag = (EditText) findViewById(R.id.derBetrag);
Betrag.setText("0");
TextView Anzeige = (TextView) findViewById(R.id.textView4);
Anzeige.setText("0");
File f = getFilesDir();
Log.d(TAG, "getFilesDir(): " + f.getAbsolutePath());
Button aktualisieren = (Button) findViewById(R.id.button2);
aktualisieren.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText Betrag = (EditText) findViewById(R.id.derBetrag);
TextView Anzeige = (TextView) findViewById(R.id.textView4);
int kosten = Integer.parseInt(load());
int einrtag = Integer.parseInt(Betrag.getText().toString());
kosten = kosten+ einrtag;
Anzeige.setText(kosten);
Betrag.setText("");
}
});
Button hinzufuegen = (Button) findViewById(R.id.button1);
hinzufuegen.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText Betrag = (EditText) findViewById(R.id.derBetrag);
TextView Anzeige = (TextView) findViewById(R.id.textView4);
int aktuell = Integer.parseInt(Anzeige.getText().toString());
int neu = aktuell + Integer.parseInt(Betrag.getText().toString());
Anzeige.setText(neu);
save(Anzeige.getText().toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_spar_rechner, menu);
return true;
}
private String load() {
StringBuilder sb = new StringBuilder();
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = openFileInput(FILENAME);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String s;
while ((s = br.readLine()) != null) {
if (sb.length() > 0) {
sb.append('\n');
}
sb.append(s);
}
} catch (Throwable t) {
Log.e(TAG, "load()", t);
} finally {
if (br != null) {
try {
br.close();
}
catch (IOException e) {
Log.e(TAG, "br.close()", e);
}
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
Log.e(TAG, "isr.close()", e);
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
Log.e(TAG, "fis.close()", e);
}
}
return sb.toString();
}
private void save(String text) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try{
fos = openFileOutput(FILENAME, MODE_PRIVATE);
osw = new OutputStreamWriter(fos);
osw.write(text);
} catch (Throwable t) {
Log.e(TAG, "save()", t);
} finally{
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
Log.e(TAG, "osw.close()", e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e){
Log.e(TAG, "fos.close()", e);
}
}
}
}
}
Alles anzeigen
und für die Oberfläche habe ich den Code:
HTML
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="146dp"
android:layout_marginRight="22dp"
android:text="@string/Hinzufuegen" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"
android:text="@string/Alles"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/derBetrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignParentRight="true"
android:layout_marginBottom="34dp"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/Heading"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toLeftOf="@+id/button1"
android:text="@string/Aktualisieren" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/derBetrag"
android:layout_alignBottom="@+id/derBetrag"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="@string/Betrag"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/textView2"
android:text="@string/Gesammt"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Alles anzeigen