Android studio intent app不断崩溃

时间:2016-09-08 18:22:55

标签: java android-studio android-intent

我的应用有问题。

问题是每次我运行我的应用程序时都会因为意图而崩溃。 我认为这是因为我的其他代码,因为如果我尝试在空白项目中以相同的方式运行相同的意图,它确实有效。

这是我的代码:

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();
}

}

我遇到了错误:遗憾的是,当我点击按钮进入下一个屏幕时,购物清单已停止,这是我的错误日志:

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) 

1 个答案:

答案 0 :(得分:0)

首先查看引用您编写的类和方法的第一行的stacktrace:

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

这告诉我们看一下CompletedActivity.java的第28行。 stacktrace中的前一行告诉我这是

Collections.sort(shoppingList);

问题在于shoppingList。你需要多挖一点才能找出原因。