2016-09-08 4 views
0

Ich habe ein Problem mit meiner App.Android Studio Intent App stürzt ab

Das Problem ist, dass jedes Mal, wenn ich meine App stürzt es wegen der Absicht abstürzt. Ich denke, dass es wegen meines anderen Codes ist, weil, wenn ich versuche, die gleiche Absicht in der gleichen Weise in einem leeren Projekt auszuführen, funktioniert es.

package com.rickhuisman.shoppinglist; 

public class MainActivity extends AppCompatActivity { 
ArrayList<String> shoppingList; 
ArrayAdapter<String> adapter; 
ListView lv; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    shoppingList = getArrayVal(getApplicationContext()); 
    Collections.sort(shoppingList); 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shoppingList); 
    lv = (ListView) findViewById(R.id.lvShowText); 
    lv.setAdapter(adapter); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View view, final int position, long id) { 
      String selectedItem = ((TextView) view).getText().toString(); 
      if (selectedItem.trim().equals(shoppingList.get(position).trim())) { 
       removeElement(selectedItem, position); 
      } else { 
       Toast.makeText(getApplicationContext(),"Error Removing Element", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

public void onClickIntent(View v) { 
    Intent intent = new Intent(this, CompletedActivity.class); 
    startActivity(intent); 
}; 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 


    if (id == R.id.action_completed) { 
     return true; 
    } 


    if (id == R.id.action_sort) { 
     //Collections.sort(shoppingList); 
     //lv.setAdapter(adapter); 
     return true; 
    } 
    if (id == R.id.action_add) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Add Item"); 
     final EditText input = new EditText(this); 
     builder.setView(input); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int which) { 

       shoppingList.add(preferredCase(input.getText().toString())); 
       Collections.sort(shoppingList); 
       storeArrayVal(shoppingList, getApplicationContext()); 
       lv.setAdapter(adapter); 
      } 
     }); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int which) { 
       dialogInterface.cancel(); 
      } 
     }); 
     builder.show(); 
     return true; 

    } 
    if (id == R.id.action_clear) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Clear Entire List"); 
     builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int which) { 
       shoppingList.clear(); 
       lv.setAdapter(adapter); 
      } 
     }); 
     builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       dialogInterface.cancel(); 
      } 
     }); 
     builder.show(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public static String preferredCase(String original) { 
    if (original.isEmpty()) 
     return original; 
     return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase(); 
}; 

public static void storeArrayVal(ArrayList<String> inArrayList, Context context) 
{ 
    Set<String> WhatToWrite = new HashSet<String>(inArrayList); 
    SharedPreferences WordSearchPutPrefs = context.getSharedPreferences("dbArrayValues", Activity.MODE_PRIVATE); 
    SharedPreferences.Editor prefEditor = WordSearchPutPrefs.edit(); 
    prefEditor.putStringSet("myArray", WhatToWrite); 
    prefEditor.commit(); 
} 

public static ArrayList getArrayVal(Context dan) 
{ 
    SharedPreferences WordSearchGetPrefs = dan.getSharedPreferences("dbArrayValues", Activity.MODE_PRIVATE); 
    Set<String> tempSet = new HashSet<String>(); 
    tempSet = WordSearchGetPrefs.getStringSet("myArray", tempSet); 
    return new ArrayList<String>(tempSet); 
} 

public void removeElement(String selectedItem, final int position){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Remove or complete " + selectedItem + "?"); 
    builder.setPositiveButton("Complete", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    builder.setNegativeButton("Remove", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      shoppingList.remove(position); 
      Collections.sort(shoppingList); 
      storeArrayVal(shoppingList, getApplicationContext()); 
      lv.setAdapter(adapter); 
     } 
    }); 
    builder.show(); 
} 

} 

Ich habe den Fehler::

Hier mein Code ist leider Shopping List zum Stillstand gekommen ist, wenn ich auf die Schaltfläche klicken, um zum nächsten Bildschirm ist log mein Fehler hier zu gehen:

09-08 19:20:55.499 2367-2367/com.rickhuisman.shoppinglist E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.rickhuisman.shoppinglist, PID: 2367 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rickhuisman.shoppinglist/com.rickhuisman.shoppinglist.CompletedActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference 


at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference 
    at java.util.Collections.sort(Collections.java:1869) 
    at com.rickhuisman.shoppinglist.CompletedActivity.onCreate(CompletedActivity.java:28) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
    at android.app.ActivityThread.-wrap11(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5417)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

Welchen Fehler bekommen Sie? – Reinard

+0

Ich habe den Beitrag bearbeitet. –

+1

'shoppingList' ist null. Es stürzt bei 'Collections.sort (shoppingList) ab;' – piotrek1543

Antwort

0

zuerst die Stacktrace für die erste Zeile nach unten schauen, die eine Klasse referenziert und Methode, die Sie schrieb:

at com.rickhuisman.shoppinglist.CompletedActivity.onCreate(CompletedActivity.java:28) 

Dies sagt uns, dass wir Zeile 28 von CompletedActivity.java betrachten sollen. Die vorhergehende Zeile in der Stacktrace sagt mir, das ist

Collections.sort(shoppingList); 

Das Problem ist also mit shoppingList. Sie müssen ein wenig mehr graben, um herauszufinden, warum.

Verwandte Themen