Ich versuche, meiner Android-Anwendung eine benutzerdefinierte Navigationsleiste hinzuzufügen. Was ich brauche, ist eine Listenansicht hinzuzufügen, die Symbol, Name und ein Kontrollkästchen enthält.Fehler beim Hinzufügen von Kontrollkästchen zur Navigationsleiste ListView
Momentan konnte ich ein Symbol und einen Namen hinzufügen.
Das ist mein onCreateView
in NavigationDrawerFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
final TypedArray typedArray = getResources().obtainTypedArray(R.array.sections_icons);
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
getResources().getStringArray(R.array.sections)
) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
int resourceId = typedArray.getResourceId(position, 0);
Drawable drawable = getResources().getDrawable(resourceId);
((TextView) v).setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
return v;
}
});
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
Aber ich konnte nicht das Kontrollkästchen meiner Navigationsleiste hinzuzufügen. Also, ich folgte this und ich versuchte, Kontrollkästchen hinzufügen, indem mDrawerList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Dies gibt mir den folgenden Fehler.
05-05 11:24:49.537 3640-3640/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 3640
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myayubo.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Jemand bitte sagen Sie mir, wie Sie Kontrollkästchen zu meinem Code hinzufügen.
Vielen Dank im Voraus. :)
Sie meinen, ich muss anderen Code in OnCreateView entfernen und das hinzufügen? –
@QualityProducts, sollten Sie Listview wie meinen Code binden –
Es tut mir leid, diese Frage zu stellen. Ich verstehe es nicht, seit ich neu bei Android bin. Kannst du beschreibender sein? Kannst du meine Antwort bekommen und bearbeiten? Vielen Dank. :) –