我正在使用數據庫示例作爲參考來幫助我爲學校創建移動應用程序,並且遇到了問題。我正在使用listView來存儲所有的字段,我希望它顯示在一個新的活動中。現在,EditTexts都在主屏幕上垂直排列,listView顯示在屏幕的底部,但是當單擊按鈕時,我希望它顯示在不同的屏幕上。第二項活動正確顯示所有信息,但主屏幕上的listView也是如此。當我嘗試擺脫主屏幕上的listView時,它不起作用。任何幫助將我指向正確的方向將是偉大的!將數據庫值傳遞到新活動
在我activity_main,我在屏幕底部的ListView控件如下:
<ListView
android:id="@+id/contactlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
而且我還宣稱它在我的activity_display_database佈局,我想它顯示出來:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/contactlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
而我的主要活動有:在那裏我居然發現了錯誤,當我從我的主XML文件中刪除列表視圖:
public class MainActivity extends AppCompatActivity {
private List<PhoneBook> contactList;
private ListView listView;
private TextView name, number, address, orderTotal, amountReceived, tip, mileage, grandTotal;
private Button add;
private PhonebookAdapter adapter;
PhoneBookHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.contactlist);
db = new PhoneBookHandler(this);
contactList = db.getAllContacts();
adapter = new PhonebookAdapter(this, contactList);
listView.setAdapter(adapter);
}
public void onClick(View view){
name = (EditText) findViewById(R.id.name);
number = (EditText) findViewById(R.id.number);
address = (EditText) findViewById(R.id.address);
orderTotal = (EditText) findViewById(R.id.orderTotal);
amountReceived = (EditText) findViewById(R.id.amountReceived);
tip = (EditText) findViewById(R.id.tip);
mileage = (EditText) findViewById(R.id.mileage);
grandTotal = (EditText) findViewById(R.id.grandTotal);
String cName = name.getText().toString();
String num = number.getText().toString();
String cAddress = address.getText().toString();
String cOrderTotal = orderTotal.getText().toString();
String cAmountReceived = amountReceived.getText().toString();
String cTip = tip.getText().toString();
String cMileage = mileage.getText().toString();
String cGrandTotal = grandTotal.getText().toString();
int id = db.addContact(new PhoneBook(cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
contactList.add(new PhoneBook(id, cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
adapter.notifyDataSetChanged();
}
public void buttonClickFunction(View v)
{
Intent intent = new Intent(getApplicationContext(), display_database.class);
startActivity(intent);
}
}
我只是試圖讓代碼在數據庫條目將顯示在activity_display_database xml文件中,而不是在主屏幕上。每當我嘗試從主屏幕刪除listView,我的主要onCreate方法中出現錯誤。感謝您的時間。任何人都可以幫助我,我會非常感激!
編輯:我從logcat中得到的錯誤如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.boley.databaseexample/com.example.boley.databaseexample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.boley.databaseexample.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
你可以發佈你收到的錯誤嗎? – BlackHatSamurai
是的,沒問題。我用logcat更新了我的問題。 –