Im Aufbau auf meiner App und ich habe diesen Fehler auf der Konsole "java.lang.RuntimeException: Kann nicht starten ComponentInfo {com.beispiel.cassieleong.delishcart/com.example.cassieleong.delishcart.FruitMain }: java.lang.NullPointerException: Der Versuch, auf einer null-Objekt Referenz“Fehler in Android App
virtuelle Methode‚Leeren android.widget.ListView.setAdapter (android.widget.ListAdapter)‘aufzurufenich versuche, eine Aktivität zu erstellen, die Listview enthalten mit checkboxtextview.
Ich weiß nicht, wie man diesen Fehler löst. Bitte helfen Sie. Danke vielmals.
Java.
public class FruitMain extends AppCompatActivity {
ListView listView;
String[] fruit;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit_main);
context = this;
listView = (ListView) findViewById(R.id.listView4);
String [] fruit = {"Apple","Applesauce","Apricot","Banana","Barlett Pears","Blackberries","Blueberries","Cantaloupe","Cherry","Citron","Coconut","Cranberry",
"Currants","Dates","Dried Fruit","Fig","Fruit Cocktail","Fruit Sauce","Grape","Grapefruit","Green Grapes","Honeydew Melon","Juniper Berries","Kiwi",
"Kumquat","Lemon","Lime","Lime Juice","Mango","Nectarines","Orange","Papaya","Passion Fruit","Peach","Pear","Persimmon",
"Pineapple","Plantain","Plum","Plum Tomatoes","Pomegranate","Prunes","Raison","Raspberry","Red Grapes","Strawberry","Sweet Cherries","Tangerine",
"Watermelon"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.fruititem_list, fruit);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds item to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_ingredient_choices, menu);
return true;
}
}
XML.
<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="com.example.cassieleong.delishcart.FruitMain"
tools:showIn="@layout/activity_fruit_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Fruit Product "
android:id="@+id/textView6"
android:textSize="35dp"
android:background="#f97c7c"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView5"
android:layout_below="@+id/textView6"
android:layout_alignParentStart="true"
android:layout_above="@+id/button2"
android:layout_alignParentEnd="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button3"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
XML
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView4"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
android:textSize="25dp">
Es funktioniert perfekt! Danke für den Hinweis auf den kleinen Fehler, den ich gemacht habe! –