I tried to view profile of the user that login. But I get a tremendouos error and my application stop working. I am new to this android development. I try to use listview in JSON and MySQL
this is my ViewProfileActivity
package com.teradata.sb186103.cbet;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ViewProfileActivity extends AppCompatActivity {
private JSONParser jsonParser;
private static String KEY_SUCCESS = "success";
String id;
String consId;
private EditText etName;
private EditText etPassword;
private EditText etEmail;
String name;
String email;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
String PREFS_NAME = "LOGIN";
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
consId = settings.getString("consId", "");
Intent intent = getIntent();
id = intent.getStringExtra(LoginActivity.ID);
etName = (EditText) findViewById(R.id.tv3);
etEmail = (EditText) findViewById(R.id.tv5);
etPassword = (EditText) findViewById(R.id.tv7);
try {
String[] arr = GetConsumerDetails(id);
String val1 = arr[0];
String val2 = arr[1];
String val3 = arr[2];
etName.setText(val1);
etEmail.setText(val2);
etPassword.setText(val3);
} catch (Exception e) {
e.printStackTrace();
}
}
public void Update(View v) {
try {
name = etName.getText().toString();
email = etEmail.getText().toString();
password = etPassword.getText().toString();
Boolean update = UpdateProfile(id, name, email, consId, password);
if(update==true){
Toast.makeText(getApplicationContext(), "PROFILE HAS BEEN UPDATED", Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(v.getContext(), CreateProfile2Activity.class);
//startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "ERROR OCCURRED!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean UpdateProfile(String id, String name, String email, String consId, String password) throws Exception{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String URL = JSONParser.serverURL;
String tag = "updateProfile";
jsonParser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
String value = id + "<@>" + name + "<@>" + email + "<@>" + consId + "<@>" + password;
params.add(new BasicNameValuePair("tag", tag));
params.add(new BasicNameValuePair("value", value));
JSONObject json = jsonParser.getJSONFromUrl(URL, params);
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1){
return true;
}
else {
return false;
}
}
public String[] GetConsumerDetails(String id) throws Exception{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String URL = JSONParser.serverURL;
String tag = "getConsumerDetails";
jsonParser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
String value = id;
params.add(new BasicNameValuePair("tag", tag));
params.add(new BasicNameValuePair("value", value));
JSONObject json = jsonParser.getJSONFromUrl (URL, params);
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
String list = json.getString("list");
String[] arr = list.split("#");
return arr;
}
else {
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void done(View v)
{
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
}
public void bankDetails(View v)
{
Intent intent = new Intent(this, CreateProfile2Activity.class);
startActivity(intent);
}
}
This is my index.php. Please refer to the Get Consumer Details tag. I got an error at while ($row = mysql_fetch_array($result)).
<?php
require_once ('database.php');
date_default_timezone_set('Asia/Kuala_Lumpur');
if (isset($_POST['tag']) && $_POST['tag'] != '') {
$tag = $_POST['tag'];
$response = array("tag" => $tag, "success" => 0);
//LOGIN
if ($tag == 'consumerLogin') {
$value = $_POST['value'];
$array = explode("<@>", $value);
$consEmail = $array[0];
$password = $array[1];
$query = "select * from consumer where consEmail='$consEmail' and password='$password'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if($row[0] != null)
$response["success"] = 1;
else
$response["success"] = 0;
echo json_encode($response);
return;
}
//GET ROW ID
else if ($tag == 'getID') {
$value = $_POST['value'];
$array = explode("<@>", $value);
$table = $array[0];
$column = $array[1];
$val = $array[2];
$query = "select id from $table where $column = '$val'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if($row[0] != null) {
$response["value"] = $row[0];
$response["success"] = 1;
}
else {
$response["success"] = 0;
}
echo json_encode($response);
return;
}
//CHECK PROFILE
else if ($tag == 'CheckProfile') {
$value = $_POST['value'];
$array = explode("<@>", $value);
$consName= $array[0];
$consEmail = $array[1];
$password = $array[2];
$query = ("select * from consumer where consEmail=$consEMail") or die(mysql_error());
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$found=false;
if($row[0]!=null)
$found=true;
if($found==true) {
$response["success"] = 1;
}
else {
$response["success"] = 0;
}
echo json_encode($response);
return;
}
//REGISTER PROFILE
else if ($tag == 'RegisterProfile') {
$value = $_POST['value'];
$array = explode("<@>", $value);
$id= $array[0];
$consName = $array[1];
$consEmail = $array[2];
$password = $array[3];
$query = "insert into consumer values(null,'$consName','$consEmail','$password')";
$result = mysql_query($query);
$response["success"] = 1;
echo json_encode($response);
return;
}
//GET CONSUMER DETAILS
else if ($tag == 'getConsumerDetails') {
$value = $_POST['value'];
$query = "select consName, consEmail, password from consumer where id=$value";
$result = mysql_query($query);
$list="";
while ($row = mysql_fetch_array($result)) {
$list = $list . $row['consName'] . "#" . $row['consEmail'] . "#" . $row['password'] ."<@>";
}
$response["success"] = 1;
$response["list"] = $list;
echo json_encode($response);
return;
}
else {
echo "Error Message: Invalid Request";
}
}
else {
echo "Error Message: Anonymous Access Denied";
}
?>
For the logcat. I got many error even saying that Android FATAL EXCEPTION MAIN: java.lang.RuntimeException: Unable to start activity ComponentInfo.
10-10 02:33:46.589 10541-10541/com.teradata.sb186103.cbet I/Choreographer: Skipped 398 frames! The application may be doing too much work on its main thread.
10-10 02:33:46.839 10541-10556/com.teradata.sb186103.cbet E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae831310
10-10 02:33:49.628 10541-10541/com.teradata.sb186103.cbet D/AndroidRuntime: Shutting down VM
10-10 02:33:49.628 10541-10541/com.teradata.sb186103.cbet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teradata.sb186103.cbet, PID: 10541
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teradata.sb186103.cbet/com.teradata.sb186103.cbet.ViewProfileActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at com.teradata.sb186103.cbet.ViewProfileActivity.onCreate(ViewProfileActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
This is my activity_view_profile.xml
<?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="com.teradata.sb186103.cbet.ViewProfileActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="your profile"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginBottom="32dp"
android:fontFamily="sans-serif-medium"
android:textColor="#B71C1C"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:layout_below="@+id/tv1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAllCaps="true"/>
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_round"
android:layout_marginTop="16dp"
android:text="fffff"
android:layout_below="@+id/tv2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:maxLength="50"
android:maxLines="1"
android:singleLine="true"
/>
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv3"
android:text="Email"
android:textAllCaps="true"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_round"
android:layout_marginTop="16dp"
android:layout_below="@+id/tv4"
android:inputType="textEmailAddress"
android:text="gggggg"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/tv6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv5"
android:text="Password"
android:textAllCaps="true"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/tv7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_round"
android:layout_marginTop="16dp"
android:layout_below="@+id/tv6"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="xxxx"
android:inputType="textPassword"
android:maxLength="12"
/>
<TextView
android:id="@+id/tv8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv7"
android:layout_marginTop="24dp"
android:text="Update Your Bank Details"
android:textAllCaps="false"
android:textColor="@android:color/holo_blue_dark"
android:onClick="bankDetails"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv8"
android:layout_marginTop="16dp"
android:background="@drawable/red_button"
android:text="Done"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:onClick="Update"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>