2017-04-14 20 views
1

我正在使用數據庫示例作爲參考來幫助我爲學校創建移動應用程序,並且遇到了問題。我正在使用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)  
+0

你可以發佈你收到的錯誤嗎? – BlackHatSamurai

+0

是的,沒問題。我用logcat更新了我的問題。 –

回答

1

,你需要擺脫的listView.setAdapter(adapter);行代碼。當你擺脫xml中的列表視圖時,你得到這個錯誤的原因是因爲你擺脫了listview引用,然後當你調用你的main方法時,該對象是null。您沒有收到編譯錯誤的原因是因爲您有另一個具有相同ID的列表視圖。如果你在數據庫中的XML來更改列表視圖,以這樣的:

<?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_database" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

,然後被刪除的主要活動的XML文件,你會得到一個編譯錯誤。我希望這是有道理的。如果您只想設置數據庫列表視圖,那麼您需要調用該列表視圖並在第二個活動中設置適配器。

+0

我想我只是不知道爲什麼我只是從我的主要xml文件註釋掉listView時出現錯誤。在我的display_database java文件中,我所做的只是複製並粘貼主Activity的onCreate方法中的內容。即使Main和activity_display_database的xml文件具有與listView相同的id名稱,它們都將正常運行。我只是想從我的主要xml中刪除listView,但那是當它拋出錯誤。 –

+0

我告訴過你爲什麼在註釋掉主xml中的listview時出錯。我也告訴過你如何解決它。如果你在主xml中註釋掉了listview,那麼在'onCreate'方法中註釋掉'listView.setAdapter(adapter)'這一行,你就可以運行該程序。在你的main xml中註釋掉listview會創建一個空指針exeption。爲了避免這種情況發生,你可以在main的'oncreate'方法中刪除'listView.setAdapter(adapter)' – BlackHatSamurai

+0

對不起,我想我沒有看到你的編輯。這工作完美!現在這讓我瘋狂了兩個星期。我只是想確保在我來到這裏之前我無法找到答案。非常感謝你的幫助! –

0

使用對象傳遞數據,在B類例如創建一個類的實例: 你有兩個類的類A和B類

Class A { 
public int id = YourID; 

public int getId() 
    return id; 
} 

而在第二類中,你可以使用這樣的ID:

Class B{ 

    A a = new A(); 
    int recievedId = a.getId // Now the id is saved in recievedId attribute   

} 

希望如果要刪除主列表視圖,可以幫助

相關問題