启动活动会显示Android - NullPointerException:storage == null

时间:2016-07-15 08:36:13

标签: java android

我有一个应用程序,当一个活动开始时显示nullpointer异常,但我无法理解错误是什么以及我做错了什么。我是android的新手,如果你能提供帮助,我将不胜感激。在此先感谢。

这是在启动AccountsActivity.java

时显示错误的活动
public class AccountsActivity extends AppCompatActivity {

 SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(OpeningScreenActivity.getContextOfApplication());
 String token = myPrefs.getString("GCMTOKEN", "");
 String JSON_URL = "http://xyz.in/view_json.php?device_id=" + token;

private ListView listView;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accounts);
    listView = (ListView) findViewById(R.id.listView);

    sendRequest();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void sendRequest() {
    StringRequest stringRequest = new StringRequest(JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(AccountsActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}



private void showJSON(String json) {
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();
    CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.mem_codes,ParseJSON.created_ons);
    listView.setAdapter(cl);
}
}

这是ParseJSON.java类

public class AccountsActivity extends AppCompatActivity {

 SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(OpeningScreenActivity.getContextOfApplication());
 String token = myPrefs.getString("GCMTOKEN", "");
 String JSON_URL = "http://sovran.in/sov2fa_v2/view_json.php?device_id=" + token;

private ListView listView;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accounts);
    listView = (ListView) findViewById(R.id.listView);

    sendRequest();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void sendRequest() {
    StringRequest stringRequest = new StringRequest(JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(AccountsActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}



private void showJSON(String json) {
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();
    CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.mem_codes,ParseJSON.created_ons);
    listView.setAdapter(cl);
}
}

这是CustomList.java

public class CustomList extends ArrayAdapter<String> {
private String[] ids;
private String[] mem_codes;
private String[] created_ons;
private Activity context;



public CustomList(Activity context, String[] ids, String[] mem_codes, String[] created_ons) {
    super(context, R.layout.list_view_layout, ids);
    this.context = context;
    this.ids = ids;
    this.mem_codes = mem_codes;
    this.created_ons = created_ons;


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
    TextView textViewId = (TextView) listViewItem.findViewById(R.id.textViewId);
    TextView textViewMemCode = (TextView) listViewItem.findViewById(R.id.textViewMem_code);
    TextView textViewCreatedOn = (TextView) listViewItem.findViewById(R.id.textViewCreated_on);
    textViewId.setText(ids[position]);
    textViewMemCode.setText(mem_codes[position]);
    textViewCreatedOn.setText(created_ons[position]);
    return listViewItem;
}


}

这是错误日志

07-15 14:04:46.269 32255-32255/com.sovran.sov2fa E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.sovran.sov2fa, PID: 32255
                                                               java.lang.ExceptionInInitializerError
                                                                   at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                   at java.lang.Class.newInstance(Class.java:1606)
                                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                   at android.os.Looper.loop(Looper.java:194)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5653)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                   at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:383)
                                                                   at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:378)
                                                                   at com.sovran.sov2fa.AccountsActivity.<clinit>(AccountsActivity.java:20)
                                                                   at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                   at java.lang.Class.newInstance(Class.java:1606) 
                                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1071) 
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614) 
                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                   at android.os.Looper.loop(Looper.java:194) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5653) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:0)

myPrefs - 方法

中初始化onCreate()属性
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_accounts);
     myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
     ...
}