这是用于创建购物清单的编码,当用户点击这些商品时,可以对其进行编辑或删除。
我按照web中给出的示例进行操作但由于某种原因,应用程序在它执行任何操作之前就停止了。我已经检查了几次,但似乎无法找到任何解决方案。我错过了什么吗?谢谢
MainActivity.java
public class MainActivity extends AppCompatActivity{
ArrayList<String> shoppingList=null;
ArrayAdapter<String> adapter= null;
ListView lv=null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
storeArrayVal(shoppingList, getApplicationContext());
Collections.sort(shoppingList);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,shoppingList);
lv = (ListView) findViewById(android.R.id.list);
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 menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id=item.getItemId();
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 dialog, int which) {
shoppingList.add(preferredCase(input.getText().toString()));
Collections.sort(shoppingList);
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.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 " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.remove(position);
Collections.sort(shoppingList);
storeArrayVal(shoppingList, getApplicationContext());
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shoppinglist">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
错误消息
12-23 23:36:43.038 1259-1259/? D/dalvikvm: Late-enabling CheckJNI
12-23 23:36:43.594 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-23 23:36:43.626 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
12-23 23:36:43.626 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve interface method 17907: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-23 23:36:43.630 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-23 23:36:43.646 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
12-23 23:36:43.650 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve interface method 17911: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-23 23:36:43.654 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-23 23:36:43.786 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-23 23:36:43.790 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve virtual method 432: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-23 23:36:43.794 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-23 23:36:43.794 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-23 23:36:43.798 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve virtual method 454: Landroid/content/res/TypedArray;.getType (I)I
12-23 23:36:43.798 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-23 23:36:43.826 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
12-23 23:36:43.830 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve direct method 18422: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
12-23 23:36:43.830 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
12-23 23:36:43.834 1259-1259/com.example.shyan.shoppinglist I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
12-23 23:36:43.834 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: VFY: unable to resolve direct method 18422: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
12-23 23:36:43.838 1259-1259/com.example.shyan.shoppinglist D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist D/AndroidRuntime: Shutting down VM
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4ba9648)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: FATAL EXCEPTION: main
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shyan.shoppinglist/com.example.shyan.shoppinglist.MainActivity}: java.lang.NullPointerException
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: Caused by: java.lang.NullPointerException
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at com.example.shyan.shoppinglist.MainActivity.onCreate(MainActivity.java:46)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5133)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 23:36:43.966 1259-1259/com.example.shyan.shoppinglist E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
问题在这一行,
storeArrayVal(shoppingList, getApplicationContext());
您应该在传递之前初始化数组。
ArrayList<String> shoppingList=null;
应该是 ArrayList shoppingList = new ArrayList();
OR 替换你的行,
storeArrayVal(shoppingList, getApplicationContext());
用,
shoppingList=new ArrayList();
storeArrayVal(shoppingList, getApplicationContext());