0

App is Crashing whenvever Click on "Save" button i have used 2 activities in this program using shared preference and GSON.. in first activity i have used 4 EditText and one Button (Submit) which will lead to 1st activity to 2nd activity in which I made 2 Buttons (Save)(Load).. During 1st activity everything is fine but in 2nd activity whenever i click on save button eventually the error is appeared as app_Closed.. I am beginner in the world of Android Development.. Senior plz help me out!!

package com.example.rc.sharprejson;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {

EditText Name, Profession, ProfId, Role;
Button Save;

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

    Name = (EditText) findViewById(R.id.editText);
    Profession = (EditText) findViewById(R.id.editText2);
    ProfId = (EditText) findViewById(R.id.editText3);
    Role = (EditText) findViewById(R.id.editText4);
    Save = (Button) findViewById(R.id.button);

    Save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent( 
Main2Activity.this,MainActivity.class);
            startActivity(intent);
        }
    });

}

}

2nd Activity

package com.example.rc.sharprejson;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {


private static final String TAG = MainActivity.class.getSimpleName();
private TextView txvDisplay;



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

    txvDisplay = (TextView) findViewById(R.id.txvdisplay);


}

public void saveObjectType(View view) {




    Main2Activity employee = getEmployee();

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    Gson gson = new Gson();

    String jsonString =  gson.toJson( employee, Main2Activity.class);
    Log.i(TAG + " Save", jsonString);

    editor.putString("employee_key", jsonString);
    editor.apply();




}

public void loadObjectType(View view) {

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    String jsonString = sharedPreferences.getString("employee_key","N/A");
    Log.i(TAG + " Load", jsonString);

    Gson gson = new Gson();
    Main2Activity employee = gson.fromJson(jsonString,Main2Activity.class);

    String Displaytext = employee.Name.getText().toString() + " \n " + employee.Profession.getText().toString() + " \n "
                      +  employee.ProfId.getText().toString();

    txvDisplay.setText(Displaytext);


}


public  Main2Activity getEmployee(){

    Main2Activity employee = new  Main2Activity();

     employee.Name.getText().toString();
     employee.Profession.getText().toString();
     employee.ProfId.getText().toString();

    return employee;
}   `
Azzabi Haythem
  • 2,056
  • 6
  • 21
  • 27
  • You cannot instantiate an activity using the new keyword, so my money is on getEmployee() causing the crash. Think you should look into starting your second activity with an Intent that carry the values you need. Think you can search google for passing data between activities for details. – cYrixmorten Jun 27 '18 at 19:07
  • thanks cYrixmorten.. so what should to do..replacing getEmployee() – Faheem Shah Jun 27 '18 at 19:32
  • Something like this: https://www.google.dk/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application&ved=2ahUKEwjG-dnzyvTbAhVHhqYKHXSqAAoQjjgwAHoECAMQAQ&usg=AOvVaw0Q2cAfq-AEEvY1Z7nii0kb – cYrixmorten Jun 27 '18 at 19:34
  • you are create a NEW employee... and then trying to GET an employee.... you can get something that isnt there. the logic in getEmployee() is wrong.... but, dont you have logs too? which will tell you exactly what the error is. – letsCode Jun 27 '18 at 19:49
  • cYrixmorten thnx i m trying what u said.. DroiDev i didnt find any compiler error... – Faheem Shah Jun 27 '18 at 19:54
  • If you are using android studio then look for the LogCat tab at the bottom to get runtime logs, and hopefully also a stack trace when the app crashes. – cYrixmorten Jun 28 '18 at 09:00

0 Answers0