Can someone help understand what I am doing wrong her with my andriod applicatio
ID: 667097 • Letter: C
Question
Can someone help understand what I am doing wrong her with my andriod application??
for some reason, the method public void saveData(View v) {
keeps showing up as error and I don't know how to fix it....Can someone take a look at my work and help me with correcting it?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText name;
private EditText size;
private EditText color;
private EditText shape;
private EditText value;
private TextView txtFound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
name = (EditText) findViewById(R.id.txtName);
size = (EditText) findViewById(R.id.txtSize);
color = (EditText) findViewById(R.id.txtColor);
shape = (EditText) findViewById(R.id.txtShape);
value = (EditText) findViewById(R.id.txtValue);
txtFound = (TextView) findViewById(R.id.txtFound);
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream fis = openFileInput("project_06");
InputStreamReader isr = new InputStreamReader(fis);
parser.setInput(isr);
int eventType = parser.getEventType();
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
String name = null;
switch (eventType) {
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("name")) {
if(parser.next() == XmlPullParser.TEXT) {
this.name.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("size")) {
if(parser.next() == XmlPullParser.TEXT) {
this.size.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("color")) {
if(parser.next() == XmlPullParser.TEXT) {
this.color.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("shape")) {
if(parser.next() == XmlPullParser.TEXT) {
this.shape.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("value")) {
if(parser.next() == XmlPullParser.TEXT) {
this.value.setText(parser.getText());
}
}
break;
}
eventType = parser.next();
}
isr.close();
fis.close();
} catch (Exception e) {
txtFound.setText("No File Found");
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.project_06, menu);
return true;
}
public void saveData(View v) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0'?>");
sb.append("<data>");
sb.append("<name>" + name.getText().toString() + "</name>");
sb.append("<size>" + size.getText().toString() + "</size>");
sb.append("<color>" + color.getText().toString() + "</color>");
sb.append("<shape>" + shape.getText().toString() + "</shape>");
sb.append("<value>" + value.getText().toString() + "</value>");
sb.append("</data>");
String fileName = "project_06";
try {
FileOutputStream fos = openFileOutput(fileName,
Context.MODE_PRIVATE);
fos.write(sb.toString().getBytes());
fos.close();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Save");
alertDialog.setMessage("Data Saved!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
} catch (IOException e) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Save");
alertDialog.setMessage("Data Not Saved!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
e.printStackTrace();
}
}
}
Explanation / Answer
I have gone through your code...nice code..but with easy fix it will be good...in saveData you are storing xml format file in stringBuilder...
theen in try block you are storing as "fos.write(sb.toString().getBytes());" but thing is here there is no need to again converting to
toString()......which throws error..
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText name;
private EditText size;
private EditText color;
private EditText shape;
private EditText value;
private TextView txtFound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
name = (EditText) findViewById(R.id.txtName);
size = (EditText) findViewById(R.id.txtSize);
color = (EditText) findViewById(R.id.txtColor);
shape = (EditText) findViewById(R.id.txtShape);
value = (EditText) findViewById(R.id.txtValue);
txtFound = (TextView) findViewById(R.id.txtFound);
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream fis = openFileInput("project_06");
InputStreamReader isr = new InputStreamReader(fis);
parser.setInput(isr);
int eventType = parser.getEventType();
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
String name = null;
switch (eventType) {
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("name")) {
if(parser.next() == XmlPullParser.TEXT) {
this.name.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("size")) {
if(parser.next() == XmlPullParser.TEXT) {
this.size.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("color")) {
if(parser.next() == XmlPullParser.TEXT) {
this.color.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("shape")) {
if(parser.next() == XmlPullParser.TEXT) {
this.shape.setText(parser.getText());
}
} else if(name.equalsIgnoreCase("value")) {
if(parser.next() == XmlPullParser.TEXT) {
this.value.setText(parser.getText());
}
}
break;
}
eventType = parser.next();
}
isr.close();
fis.close();
} catch (Exception e) {
txtFound.setText("No File Found");
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.project_06, menu);
return true;
}
public void saveData(View v) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0'?>");
sb.append("<data>");
sb.append("<name>" + name.getText().toString() + "</name>");
sb.append("<size>" + size.getText().toString() + "</size>");
sb.append("<color>" + color.getText().toString() + "</color>");
sb.append("<shape>" + shape.getText().toString() + "</shape>");
sb.append("<value>" + value.getText().toString() + "</value>");
sb.append("</data>");
String fileName = "project_06";
try {
FileOutputStream fos = openFileOutput(fileName,
Context.MODE_PRIVATE);
fos.write(sb.getBytes());
fos.close();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Save");
alertDialog.setMessage("Data Saved!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
} catch (IOException e) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Save");
alertDialog.setMessage("Data Not Saved!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
e.printStackTrace();
}
}
}