gpt4 book ai didi

java - NullPointerException 调用 getIntent().getStringExtra(...)

转载 作者:行者123 更新时间:2023-12-02 08:41:27 24 4
gpt4 key购买 nike

首先,我对编码非常陌生,而且对 Firebase 的功能也很陌生。问题是,我试图通过将数字与值关联来从 firebase 获取特定值。正如我将其设计为我的第一个 Activity (Buffer2),将序列号与 EditText 一起放置,从而将值从此 Activity 传递到下一个 Activity (Buffer3)。并分配此intent.getExtra字符串,然后将其设置为int以放置序列号。但进程崩溃了(或者停止了,说它为空)。还有另一种方法可以完成这件事吗?任何帮助将不胜感激。

这是我的 buffer2 和 buffer3 代码(xml 和 java)

buffer2 xml----------------------------------------

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Buffer2">

<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/etGo"
android:hint="Go To Page No"
android:inputType="number"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnGo"
android:text="Go"
android:textStyle="bold"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
</LinearLayout>

Buffer2 java----------------------------------------------------

public class Buffer2 extends AppCompatActivity {

Button BtnGo;
EditText EtGo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buffer2);

BtnGo=findViewById(R.id.btnGo);
EtGo=findViewById(R.id.etGo);
String number = Buffer2.this.EtGo.getText().toString();

BtnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Buffer2.this, Buffer3.class);
intent.putExtra("numberto", number);
startActivity(intent);
}
});


}
}

buffer3 xml--------------------------------------------------------

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Buffer3">

<TextView
android:id="@+id/nameTv"
android:textSize="28sp"
android:gravity="center"
android:text="Loading..."
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>

<TextView
android:id="@+id/professionTv"
android:textSize="28sp"
android:gravity="center"
android:text="Loading..."
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>

</LinearLayout>

Buffer3 java------------------------------------------

public class Buffer3 extends AppCompatActivity {

TextView Name, Profession;
DatabaseReference reference;
String Number = getIntent().getStringExtra("numberto");

int count = Integer.parseInt(String.valueOf(Number));

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buffer3);

Name = findViewById(R.id.nameTv);
Profession = findViewById(R.id.professionTv);

reference=FirebaseDatabase.getInstance().getReference().child("What")
.child(String.valueOf(count));
}

protected void onStart() {
super.onStart();

reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String type = dataSnapshot.child("type").getValue().toString();
Name.setText(name);
Profession.setText(type);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

Logcat------------------------------------

2020-04-22 18:39:48.939 11231-11231/com.potato.manpower I/Timeline: Timeline: Activity_launch_request time:419024820 intent:Intent { cmp=com.potato.manpower/.Buffer3 (has extras) }
2020-04-22 18:39:48.969 11231-11231/com.potato.manpower D/AndroidRuntime: Shutting down VM
2020-04-22 18:39:48.970 11231-11231/com.potato.manpower E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.potato.manpower, PID: 11231
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.potato.manpower/com.potato.manpower.Buffer3}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.potato.manpower.Buffer3.<init>(Buffer3.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2639)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:165) 
at android.app.ActivityThread.main(ActivityThread.java:6375) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802) 
2020-04-22 18:39:48.999 11231-11231/com.potato.manpower I/Process: Sending signal. PID: 11231 SIG: 9

Firebase 结构 enter image description here

最佳答案

看起来 getIntent() 在这一行中返回 null:

String Number = getIntent().getStringExtra("numberto");

如果您将 Number 的初始化移至 onCreate 方法中,我预计该问题将会消失:


public class Buffer3 extends AppCompatActivity {
TextView Name, Profession;
DatabaseReference reference;
String Number;

int count = Integer.parseInt(String.valueOf(Number));

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buffer3);

Number = getIntent().getStringExtra("numberto");

Name = findViewById(R.id.nameTv);
Profession = findViewById(R.id.professionTv);

reference=FirebaseDatabase.getInstance().getReference().child("What")
.child(String.valueOf(count));
}
...

其他一些注意事项:

关于java - NullPointerException 调用 getIntent().getStringExtra(...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61366216/

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