0

我正在尝试开发一个 Android 应用程序,但我遇到了其中的一部分问题。我正在尝试使用一个按钮创建一个活动,当它被单击时,它会将您发送到另一个活动,您可以在其中写一个名称,该名称将通过按钮传递回活动,并设置一个按钮的可见性设置为不可见到可见和传回的文本。我遇到的问题是,每当我尝试从应该引入字符串的类中单击“确定”按钮时,我都会收到 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)
4

3 回答 3

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)

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

于 2013-10-27T15:37:36.907 回答
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();

笔记本课上。

于 2013-10-27T15:48:11.807 回答
0

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

 NewTextInput = (EditText) findViewById(R.id.etSetSubjectName); 
于 2013-10-29T13:33:37.723 回答