Hier ist mein Code, ich weiß, es ist ein kleiner Fehler irgendwo, aber ein Noob in Android Ich kann es nicht herausfinden. Ich habe meine Suche erledigt, aber ohne Erfolg.textview.setText() in ListView verursacht Force schließen
Die SimpleListActivity.java:
public class SimpleListActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new myArrayAdapter(this, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria"
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</CheckBox>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView">
</TextView>
</LinearLayout>
myArrayAdapter.java:
public class myArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public myArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.checkBox1);
textView.setText(values[position]);
return rowView;
}
}
Ich verstehe nicht, warum wir Hauptlayout als ContentView verwenden. Ich meine, das offizielle Listview-Tutorial benutzt es nicht und die Liste funktioniert gut. Aber ich habe es im Netz gefunden, dass ich, wenn ich findViewById verwende, setContentView(R.layout.main);
verwenden muss. Ich verstehe nicht, wenn die ID nicht in der Hauptansicht ist, warum sollte ich sie dann verwenden?
Auch diese App Kraft schließt. Ich habe das Problem auf textView.setText(values[position]);
eingegrenzt. Wo gehe ich falsch?
aktualisieren: Ich entfernte setContentView wie es von denen eine andere Fehlerlösung hier gebucht geben wurde: ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly
Hier ist die logcat nach setContentView Entfernung:
03-24 10:55:01.558: E/AndroidRuntime(7279): FATAL EXCEPTION: main
03-24 10:55:01.558: E/AndroidRuntime(7279): java.lang.NullPointerException
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.deepakmittal.simplelist.myArrayAdapter.getView(myArrayAdapter.java:32)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillDown(ListView.java:670)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillFromTop(ListView.java:727)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.layoutChildren(ListView.java:1598)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.onLayout(AbsListView.java:1273)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Looper.loop(Looper.java:130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invoke(Method.java:507)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-24 10:55:01.558: E/AndroidRuntime(7279): at dalvik.system.NativeStart.main(Native Method)
Die Linie 32 entspricht Textview. setText (Werte [Position]);
Kannst du das logcat? – dcow
Hat den Logcat gepostet. –