Прежде всего, я очень новичок в программировании, а также в новых возможностях Firebase. Проблема в том, что я пытаюсь получить конкретное значение c из firebase, указав число, связанное со значением. Поскольку я спроектировал это как мое первое действие (Buffer2), поместите серийный номер в EditText и, таким образом, передайте значение из этого действия в следующее действие (Buffer3). и назначил эту строку intent.getExtra и затем сделал это как int, чтобы поместить серийный номер. Но процесс получил cra sh (или остановился, чтобы сказать, что он нулевой). Есть ли другой способ сделать это? любая помощь будет оценена.
Вот мой код (xml и java) для buffer2 и buffer3
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 введите описание изображения здесь