【发布时间】: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