所以我有一個串列視圖,顯示圣經中詹姆斯書的 5 章。當用戶選擇他想要閱讀的章節時,該章節的經文將顯示出來。我要做的是從JamesChapter.java獲取位置值以傳遞給下一個活動Bible.java并將該值用作我想要在我的 json 檔案中訪問的陣列的索引值,該陣列將顯示經文. 當我單擊章節時,會發生什么應用程式崩潰。我嘗試使用 getIntent() 和 getIntExtra() 但我不知道如何使用這些方法中的值作為陣列索引應用。感謝有關如何在下一個活動中使用上一個活動中的值到陣列的幫助。祝你有美好的一天

public class JamesChapters extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_chapters);
ListView listView = findViewById(R.id.listview);
List<String> list = new ArrayList<>();
list.add("Chapter 1");
list.add("Chapter 2");
list.add("Chapter 3");
list.add("Chapter 4");
list.add("Chapter 5");
ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(JamesChapters.this, Bible.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
}
public class Bible extends AppCompatActivity {
TextView tvReference, tvVersion, tvVerse;
String reference, version, verse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bible);
Intent intent = getIntent();
int position = intent.getIntExtra("position",0);
displayJArray(position);
}
public void displayJArray(int value){
String rid = rid();
try{
JSONObject json = new JSONObject(rid);
JSONArray jsonarray = json.getJSONArray("James");
JSONObject newjson = jsonarray.getJSONObject(value);
reference = newjson.getString("Reference");
version = newjson.getString("Version");
verse = newjson.getString("Verse");
tvReference.setText(reference);
tvVersion.setText(version);
tvVerse.setText(verse);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String rid(){
String content="";
try{
InputStream ist = getAssets().open("james.json");
int size = ist.available();
byte [] buffer = new byte[size];
ist.read(buffer);
content = new String(buffer);
}catch (IOException e) {
e.printStackTrace();
}
return content;
}
}
這是我的 json 檔案的格式
{
"James": [
{
"Reference": "James 1:1-27",
"Version": "NIV",
"Verse": "......"
},
{
"Reference": "James 2:1-26",
"Version": "NIV"
"Verse": "....."
}
]}
這是來自 logcat 的錯誤
2022-11-02 15:51:32.805 25637-25676/ph.edu.nu.soco.finalproject E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2022-11-02 15:51:42.272 25637-25637/ph.edu.nu.soco.finalproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: ph.edu.nu.soco.finalproject, PID: 25637
java.lang.RuntimeException: Unable to start activity ComponentInfo{ph.edu.nu.soco.finalproject/ph.edu.nu.soco.finalproject.Bible}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at ph.edu.nu.soco.finalproject.Bible.displayJArray(Bible.java:37)
at ph.edu.nu.soco.finalproject.Bible.onCreate(Bible.java:25)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2022-11-02 15:51:43.018 25695-25721/ph.edu.nu.soco.finalproject E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
uj5u.com熱心網友回復:
您的應用程式崩潰,因為您已定義tvReference但未初始化,然后您嘗試在displayJArray()方法中訪問它,導致運行時出現NullPointerException,因為 Android 無法在 Activity 中找到視圖。在 Method 中初始化您的 textViews,onCreate如下例所示。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bible);
// Initialize views like this
tvReference = findViewById(R.id.id_of_your_textView_in_xml);
tvVersion = same as above ;
tvVerse = same as above;
Intent intent = getIntent();
int position = intent.getIntExtra("position",0);
displayJArray(position);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/525962.html
標籤:爪哇安卓json移动应用
