2016-07-15 10 views
-1

Ich habe eine App, in der, wenn eine Aktivität gestartet wird Nullpointer Ausnahme zeigt, aber ich kann nicht verstehen, was der Fehler ist und wo ich falsch mache. Ich bin neu bei Android und ich wäre dankbar, wenn Sie mir helfen könnten. Dieser Dank in advance.`Starten einer Aktivität zeigt Android - NullPointerException: storage == null

ist die Aktivität, die den Fehler zeigt beim Start 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); 
} 
} 

Dies ist ParseJSON.java Klasse

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

Dies ist 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; 
} 


} 

Und das ist das Fehlerprotokoll

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)  

Antwort

0

Sie die Initialisierung Ihres myPrefs Attribut innerhalb onCreate() -Methode

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_accounts); 
    myPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
    ... 
} 
Verwandte Themen