ListView ArrayList + startActivityForResult

K

krackmoe

Neues Mitglied
1
Ich spiele mich gerade ein bisschen herum um mal alles bisl besser kennenzulernen.

Ich hab eine ListView, wenn ich dort auf einen Button klicke möchte ich zu einer anderen Activity geleitet werden die mir ein Ergebnis zurückliefert, was ich wiederum in die ArrayList speichern möchte die die ListView befüllt.

Mir stürzt meine App jedoch immer schon beim starten ab und ich verstehe nicht warum...

mainWindow.class:
Code:
package src.all;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class mainWindow extends Activity {
	private List<String> mButtons = new ArrayList<String>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        for(String s: getResources().getStringArray(R.array.buttons)){
        	mButtons.add(s);
        }       	
        
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, mButtons);
        ListView lv = (ListView)findViewById(R.id.myList);
    	lv.setAdapter(arrayAdapter);
    	
    	lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(position == 2){					
					Intent intent = new Intent(mainWindow.this, zweiteKlasse.class);
					startActivityForResult(intent,1);					
				}			
			}        	
        });
        
        
        
               
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == Activity.RESULT_OK && requestCode == 1){
			Bundle extras = data.getExtras();
			mButtons.add(extras.getString("name"));
		}
	}
    
}

main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<ListView android:id="@+id/myList"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>

list_item.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
 
Was sagt denn die Katze (LogCat)?
 
Oha.. maybe könnte der ListView lv = (ListView)findViewById(R.id.myList); NULL sein.

Nur warum!?
In der R.java steht myList drin...

Und hier hab ichs auch:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<ListView android:id="@+id/myList"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>
 
Hab den Fehler denke ich!

Musste noch setContentView aufrufen, in der sich die List befindet, vor allem anderen!
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.017
swa00
swa00
D
Antworten
9
Aufrufe
1.762
jogimuc
J
J
  • JoEntwickler
Antworten
0
Aufrufe
972
JoEntwickler
J
Zurück
Oben Unten