Hey Leute, also habe ein Grundlayout von einer Category-Ansicht wo ich ein List View drin habe. Aber sobald ich in die Activity wechsel, wird die App geschlossen. Habe zu dem Problem keine passende Antwort im Forum und auch nicht wirklich bei google gefunden. Kann mir wer erklären wie ich in einer normalen Activity ein ListView befüllen kann und den OnItemClickListener setzen kann?
So sieht die Activity bis jetzt aus, funktioniert aber leider nicht und wie gesagt ich finde den Fehler nicht :/
Das Layout zu der Activity sieht so aus:
[XML]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android
rientation="vertical" >
<TextView
android:id="@+id/Banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Banner"
android:textSize="40dp"
android:typeface="monospace" >
</TextView>
<TextView
android:id="@+id/headstart"
android:text="Kategorien"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:background="@color/topiccolor"/>
<ListView
android:id="@+id/CategoryList"
android:layout_width="match_parent"
android:layout_height="355dp"
android:layout_weight="0.15"
android:visibility="visible"
android:layout_margin="5dp" >
</ListView>
<LinearLayout
android
rientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageButton
android:id="@+id/HomeButtonCat"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="@drawable/ic_launcher"
android:background="#000000" />
<ImageButton
android:id="@+id/CategoryButtonCat"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="@drawable/ic_launcher"
android:background="#000000"/>
</LinearLayout>
</LinearLayout>
[/XML]
Und das listitem:
[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"
android
adding="10dp"
android:textSize="16dp">
</TextView>
[/XML]
Danke für eure Hilfe!
Java:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.content.Intent;
public class CategoryActivity extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categorylayout);
ActivityRegistry.register(this);
String[] categoryText = getResources().getStringArray(R.array.categoryString);
setListAdapter(new ArrayAdapter<String>(this, R.layout.category_item, categoryText));
ListView lv = (ListView)findViewById(R.id.CategoryList);
//getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(CategoryActivity.this, "Test", Toast.LENGTH_SHORT).show();
}
});
final ImageButton homebutton = (ImageButton) findViewById(R.id.HomeButtonCat);
final ImageButton categorybutton = (ImageButton) findViewById(R.id.CategoryButtonCat);
homebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startpageIntent = new Intent(CategoryActivity.this, StartpageActivity.class);
startActivity(startpageIntent);
}
});
categorybutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent categoryIntent = new Intent(CategoryActivity.this, CategoryActivity.class);
startActivity(categoryIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.exit:
exit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void exit(){
ActivityRegistry.finischAll();
}
}
Das Layout zu der Activity sieht so aus:
[XML]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android
<TextView
android:id="@+id/Banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Banner"
android:textSize="40dp"
android:typeface="monospace" >
</TextView>
<TextView
android:id="@+id/headstart"
android:text="Kategorien"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:background="@color/topiccolor"/>
<ListView
android:id="@+id/CategoryList"
android:layout_width="match_parent"
android:layout_height="355dp"
android:layout_weight="0.15"
android:visibility="visible"
android:layout_margin="5dp" >
</ListView>
<LinearLayout
android
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageButton
android:id="@+id/HomeButtonCat"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="@drawable/ic_launcher"
android:background="#000000" />
<ImageButton
android:id="@+id/CategoryButtonCat"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="@drawable/ic_launcher"
android:background="#000000"/>
</LinearLayout>
</LinearLayout>
[/XML]
Und das listitem:
[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"
android
android:textSize="16dp">
</TextView>
[/XML]
Danke für eure Hilfe!