-1

I've been doing an application which requires user inputs. This is my code:

UserInput.java

    public class UserInput extends Activity
{
    String tag = "UserInput";
    EditText userInput;
    Uri rResult = null;

    int request_Code = 1;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userinput);

        Button btn= (Button) findViewById(R.id.btnSubmit);



        Button saveButton = (Button) findViewById(R.id.btnSave);
        saveButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent("com.example.Summary");

                Bundle extras = new Bundle();
                extras.putString("amount", userInput.getText().toString());

                intent.putExtras(extras);

                startActivityForResult(intent, request_Code);
            }
        });
    }
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }

        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            saveAsText();
            Log.d(tag, "In the onPause() event");
        }

        protected void onRestart() {
            // TODO Auto-generated method stub
            super.onRestart();
        }


        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            retrieveText();
            Log.d(tag, "In the onResume() event");
        }


        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
        }


        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }

        public void saveAsText() {
            String line = userInput.getText().toString();
            if (rResult != null)
                line += "|" + rResult;

            FileWriter fw = null;
            BufferedWriter bw = null;
            PrintWriter pw = null;

            try {
                String path = Environment.getExternalStorageDirectory().getPath();

                fw = new FileWriter(path + "/UserInput.txt");
                bw = new BufferedWriter(fw);
                pw = new PrintWriter(bw);
                pw.println(line);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pw != null)
                        pw.close();
                    if (bw != null)
                        bw.close();
                    if (fw != null)
                        fw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }// saveAsText

        public void retrieveText() {
            FileReader fr = null;
            BufferedReader br = null;
            try {
                String line;
                String path = Environment.getExternalStorageDirectory().getPath();
                fr = new FileReader(path + "/UserInput.txt");
                br = new BufferedReader(fr);
                line = br.readLine();

                StringTokenizer st = new StringTokenizer(line, "|");

                userInput.setText(st.nextToken());


                String rResult;
                if (st.hasMoreTokens())
                    rResult = st.nextToken();
                else
                    rResult = "";

                Log.d(tag, "readAsText: " + line);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)
                        br.close();
                    if (fr != null)
                        fr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

Here is my Summary Page:

public class Summary extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary);

        Bundle bundle = getIntent().getExtras();

        int amount = Integer.parseInt(bundle.getString("amount"));

        TextView resultView = (TextView) findViewById(R.id.retrieveInput);

        resultView.setText(amount);

    }

When I run using these codes, it still runs, but when I click the User Input button, it crashed. May I know what went wrong and what I can do so that it runs perfectly? Thanks! Logcat

enter image description here

user3774763
  • 125
  • 1
  • 1
  • 8

2 Answers2

2

When you create an Intent with the String constructor, you are actually setting the Intent's action. What you want is intent = new Intent(UserInput.this, Summary.class). Also make sure that the Summary activity is registered in your manifest.

vmagro
  • 181
  • 7
  • i do not have a constructor – user3774763 Jul 29 '14 at 01:09
  • The Intent class provides a constructor that takes a String, which is what you are using, to launch your activity you have to use the code I listed in my answer. However you also need to assign a value to `userInput` as committedandroider said with `userInput = (EditText) findViewById(R.id.idOfExitText)` – vmagro Jul 29 '14 at 16:58
0

userInput, did you initiate that edit text? when you don't initiate. Its starting value is null. In that onclick, you're getting that null pointer exception bc you're trying to dereference that null object

committedandroider
  • 8,711
  • 14
  • 71
  • 126