我使用数据库示例作为参考来帮助我为学校创建移动应用,并遇到了问题。我使用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时,我的主要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 :(得分:1)
如果要删除主列表视图,则需要删除listView.setAdapter(adapter);
代码行。当你摆脱xml中的列表视图时,你得到这个错误的原因是因为你摆脱了listview引用,然后当你调用main方法时,该对象为null。您没有收到编译错误的原因是因为您有另一个具有相同ID的列表视图。如果您要将数据库xml中的listview更改为:
<?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文件,你得到编译错误。我希望这是有道理的。如果只想设置数据库列表视图,则需要调用该列表视图并在第二个活动中设置适配器。
答案 1 :(得分:0)
使用对象传递数据,在B类中创建A类实例。例如: 你有两个班级A班和班级
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
}
希望有所帮助