【问题标题】:Passing database values to new activity将数据库值传递给新活动
【发布时间】:2017-04-14 01:25:52
【问题描述】:

我使用数据库示例作为参考来帮助我为学校创建移动应用程序,但遇到了问题。我正在使用 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 文件中删除 listView 时,我实际上得到了错误:

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 时,我的 main 的 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) 

【问题讨论】:

  • 你能发布你得到的错误吗?
  • 是的,没问题。我用 logcat 更新了我的问题。

标签: android listview android-intent


【解决方案1】:

如果你想删除主列表视图,你需要去掉listView.setAdapter(adapter);这行代码。当您摆脱 xml 中的列表视图时出现该错误的原因是因为您摆脱了列表视图引用,然后当您调用 main 方法时,该对象为空。您没有收到编译错误的原因是因为您有另一个具有相同 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 文件,你会得到一个编译器错误。我希望这是有道理的。如果您只想设置数据库列表视图,那么您需要调用该列表视图并在第二个活动中设置适配器。

【讨论】:

  • 我想我只是不确定为什么当我从主 xml 文件中注释掉 listView 时才会收到错误消息。在我的 display_database java 文件中,我所做的只是从 Main Activity 的 onCreate 方法中复制并粘贴内容。即使 Main 和 activity_display_database 的 xml 文件对于 listView 具有相同的 id 名称,它们也会正常运行。我只是想从我的 Main xml 中删除 listView,但那是它引发错误的时候。
  • 我告诉过你为什么在主 xml 中注释掉列表视图时会出现错误。我也告诉你如何解决它。如果在主xml中注释掉listview,然后在onCreate方法中注释掉listView.setAdapter(adapter)这行,就可以运行程序了。在主 xml 中注释掉列表视图会创建一个空指针异常。为避免出现这种情况,请在 main 的 oncreate 方法中删除 listView.setAdapter(adapter)
  • 对不起,我想我没有看到您的编辑。效果很好!这已经让我发疯了两个星期了。我只是想确保在我来这里之前我无法在任何地方找到答案。非常感谢您的帮助!
【解决方案2】:

使用对象传递数据,在 B 类中创建 A 类的实例。例如: 你有两个类 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        

}

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多