我有兩個活動。 MainActivity.java和HomeActivity.java。我試圖在HomeActivity.java中創建一個ListView作爲List項目的「One」,「Two」,「Three」和「Four」,但ListView根本沒有顯示出來。ListView不顯示/出現android studio
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
String personName;
String email;
String rfid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
populateListView();
Bundle extras = getIntent().getExtras();
if (extras != null) {
personName = extras.getString("Name");
email = extras.getString("Email");
rfid = extras.getString("RFID");
}
String params = (email + "+" + rfid);
new MyAsyncTask().execute(new Pair<Context, String>(this, params));
toolbar.findViewById(R.id.ButtonSettings).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(HomeActivity.this, findViewById(R.id.ButtonSettings));
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.getMenu().add(personName);
popup.getMenu().add(email);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
HomeActivity.this, "You Clicked" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
content_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ie.myy3p.ticktask.HomeActivity"
tools:showIn="@layout/activity_home">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/profile_layout"
android:layout_below="@+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tView"
android:layout_alignBottom="@id/toolbar"
android:layout_alignParentEnd="true" />
<ListView android:id = "@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
show_tasks.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
編輯:
因此,我對代碼進行了一些更改,現在我的ListView顯示在Android Studio的content_home.xml和activity_home.xml中,它以前沒有做過。但是,當我在手機上運行應用程序時,ListView不顯示。我調試了它,並檢查了我的populateList方法,它似乎工作正常。調試在populateList方法的末尾寫道:
this = {[email protected]}
myItems = {String[4]@20825}
adapter = {[email protected]}
list = {[email protected]} "android.widget.ListView... etc
發佈您的logcat錯誤,如果有的話。 –