NullPointerException错误onActivityResult

时间:2013-10-27 15:36:23

标签: java android onactivityresult

我正在尝试开发Android应用,但我遇到了部分问题。 我正在尝试创建一个带有按钮的活动,当它点击它时会将您发送到另一个活动,您可以在其中编写名称,此名称将通过按钮传递回活动,并设置一个按钮的可见性。设置为对可见不可见以及传回的文本。 我遇到的问题是,每当我尝试从你应该引入字符串的类中单击ok按钮时,我会得到一个NullPointer异常。我复制/粘贴代码。

带按钮的活动(Notebook.java):

package eu.lafarga.treballderecerca;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import eu.lafarga.treballderecerca.Notebook_NewSubject;

public class Notebook extends Activity implements View.OnClickListener {

    public Button NewSubjectButton, Button1L, Button2L, Button3L, Button4L,
            Button5L, Button1R, Button2R, Button3R, Button4R, Button5R;
    private int counter;

    // onCreate
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notebook);
        initializeNotebook();


    }

    public void initializeNotebook() {
        NewSubjectButton = (Button) findViewById(R.id.bNotebookNew);
        Button1L = (Button) findViewById(R.id.bHiddenL1);
        Button2L = (Button) findViewById(R.id.bHiddenL2);
        Button3L = (Button) findViewById(R.id.bHiddenL3);
        Button4L = (Button) findViewById(R.id.bHiddenL4);
        Button5L = (Button) findViewById(R.id.bHiddenL5);
        Button1R = (Button) findViewById(R.id.bHiddenR1);
        Button2R = (Button) findViewById(R.id.bHiddenR2);
        Button3R = (Button) findViewById(R.id.bHiddenR3);
        Button4R = (Button) findViewById(R.id.bHiddenR4);
        Button5R = (Button) findViewById(R.id.bHiddenR5);

        NewSubjectButton.setOnClickListener(this);
        Button1L.setOnClickListener(this);
        Button2L.setOnClickListener(this);
        Button3L.setOnClickListener(this);
        Button4L.setOnClickListener(this);
        Button5L.setOnClickListener(this);
        Button1R.setOnClickListener(this);
        Button2R.setOnClickListener(this);
        Button3R.setOnClickListener(this);
        Button4R.setOnClickListener(this);
        Button5R.setOnClickListener(this);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bundle getNewSubjectBundle = getIntent().getExtras();
        int Number = getNewSubjectBundle.getInt("int");
        String Title = "0";

        if(resultCode == RESULT_OK && Number == 1){

            Title = getNewSubjectBundle.getString("title1L");
            Button1L.setText(Title);
            Button1L.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 2){

            Title = getNewSubjectBundle.getString("title2L");
            Button2L.setText(Title);
            Button2L.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 3){
            Title = getNewSubjectBundle.getString("title3L");
            Button3L.setText(Title);
            Button3L.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 4){
            Title = getNewSubjectBundle.getString("title4L");
            Button4L.setText(Title);
            Button4L.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 5){
            Title = getNewSubjectBundle.getString("title5L");
            Button5L.setText(Title);
            Button5L.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 6){
            Title = getNewSubjectBundle.getString("title1R");
            Button1R.setText(Title);
            Button1R.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 7){
            Title = getNewSubjectBundle.getString("title2R");
            Button2R.setText(Title);
            Button2R.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 8){
            Title = getNewSubjectBundle.getString("title3R");
            Button3R.setText(Title);
            Button3R.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 9){
            Title = getNewSubjectBundle.getString("title4R");
            Button4R.setText(Title);
            Button4R.setVisibility(View.VISIBLE);
            counter++;

        }else if(resultCode == RESULT_OK && Number == 10){
            Title = getNewSubjectBundle.getString("title5R");
            Button5R.setText(Title);
            Button5R.setVisibility(View.VISIBLE);
            counter++;

        }else if(counter > 10){
            NewSubjectButton.setVisibility(View.GONE);
        }
    }

您应该引入文本的活动(Notebook_NewSubject.java):

package eu.lafarga.treballderecerca;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Notebook_NewSubject extends Activity implements View.OnClickListener {

    private Button OKButton;
    Bundle NewSubjectBundle;
    private EditText NewTextInput;
    Intent mIntent = new Intent();

    int[] integer = {0,0,0,0,0,0,0,0,0,0};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newnotebookbutton);
        initializeNotebookNewSubject();


    }


        public void initializeNotebookNewSubject() {

            NewTextInput = (EditText) findViewById(R.id.etNewNotebookButtonCreateSubjectButton);
            OKButton = (Button) findViewById(R.id.bOkButton);

            OKButton.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {



            switch (v.getId()){

            case R.id.bOkButton:


            if(integer[0] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title1L", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 1);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[0] = 1;
                 return;


            }else if(integer[1] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title2L", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 2);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[1] = 1;
                 return;

            }else if(integer[2] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title3L", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 3);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[2] = 1;
                 return;

            }else if(integer[3] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title4L", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 4);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[3] = 1;
                 return;

            }else if(integer[4] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title5L", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 5);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[4] = 1;
                 return;

            }else if(integer[5] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title1R", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 6);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[5] = 1;
                 return;

            }else if(integer[6] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title2R", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 7);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[6] = 1;
                 return;

            }else if(integer[7] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title3R", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 8);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[7] = 1;
                 return;

            }else if(integer[8] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title4R", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 9);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[8] = 1;
                 return;

            }else if(integer[9] == 0 && NewTextInput.getText().toString() != null){

                NewSubjectBundle.putString("title5R", NewTextInput.getText().toString());
                NewSubjectBundle.putInt("int", 10);
                 mIntent.putExtras(NewSubjectBundle);
                 setResult(RESULT_OK, mIntent);
                 integer[9] = 1;
                 return;

            }


            break;
        }
            finish();
    }

}

错误:

10-27 11:25:31.536: D/AndroidRuntime(837): Shutting down VM
10-27 11:25:31.536: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-27 11:25:31.787: E/AndroidRuntime(837): FATAL EXCEPTION: main
10-27 11:25:31.787: E/AndroidRuntime(837): java.lang.NullPointerException
10-27 11:25:31.787: E/AndroidRuntime(837):  at eu.lafarga.treballderecerca.Notebook_NewSubject.onClick(Notebook_NewSubject.java:47)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.view.View.performClick(View.java:4240)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.view.View$PerformClick.run(View.java:17721)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.os.Handler.handleCallback(Handler.java:730)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
10-27 11:25:31.787: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 11:25:31.787: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
10-27 11:25:31.787: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:525)
10-27 11:25:31.787: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 11:25:31.787: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 11:25:31.787: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

Notebook_NewSubject活动中,您忘记初始化Bundle对象。

Bundle NewSubjectBundle; <- declared but not initialized

因此,在执行NewSubjectBundle.putString(...)时会抛出NullPointerException

NullPointerException通常很容易解决。

at eu.lafarga.treballderecerca.Notebook_NewSubject.onClick(Notebook_NewSubject.java:47)

基本上告诉你没有初始化的东西。所以你应该阅读这一行(即Notebook_NewSubject.java的第47行),看看可能是null

答案 1 :(得分:1)

尝试更改 Notebook_NewSubject

NewSubjectBundle.putString("title1L", NewTextInput.getText().toString());
NewSubjectBundle.putInt("int", 1);
mIntent.putExtras(NewSubjectBundle);

Intent mIntent = new Intent();
mIntent.putString("title1L", NewTextInput.getText().toString());
mIntent.putInt("int", 1);
setResult(RESULT_OK, mIntent);

Notebook班级

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String title = data.getStringExtra("title1L");
        int title = data.getIntExtra("int");
        //////

编辑: -

如果您使用的是Bundle,请更改

Bundle getNewSubjectBundle = getIntent().getExtras();

Bundle getNewSubjectBundle = data.getExtras();

NoteBook 类中。

答案 2 :(得分:0)

我终于意识到我没有正确引用带有XML id的java。因此,编辑文本应该是这样的:

 NewTextInput = (EditText) findViewById(R.id.etSetSubjectName);