将数据传递给intent时出错

时间:2014-07-29 00:49:04

标签: android android-intent adt eclipse-adt

我一直在做一个需要用户输入的应用程序。这是我的代码:

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();
                }
            }
        }
    }

以下是我的摘要页面:

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);

    }

当我使用这些代码运行时,它仍会运行,但是当我单击“用户输入”按钮时,它会崩溃。我可以知道出了什么问题,我能做些什么才能完美运行?谢谢! logcat的

enter image description here

2 个答案:

答案 0 :(得分:2)

使用String构造函数创建Intent时,实际上是在设置Intent的操作。你想要的是intent = new Intent(UserInput.this, Summary.class)。还要确保在清单中注册了摘要活动。

答案 1 :(得分:0)

userInput,您是否启动了该编辑文本?当你不开始。它的起始值为null。在那个onclick中,你正在尝试取消引用该空对象的那个空指针异常