gpt4 book ai didi

java - ActivityResult 上的 NullPointerException 错误

转载 作者:行者123 更新时间:2023-12-01 23:28:57 25 4
gpt4 key购买 nike

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

带有按钮的 Activity (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);
}
}

您应该介绍文本的 Activity (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)

最佳答案

在您的 Notebook_NewSubject Activity 中,您忘记初始化 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 的内容。

关于java - ActivityResult 上的 NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com