2014-06-24 147 views
1

我試圖運行一個簡單的應用程序使用凌空和自定義標題,它沒有錯誤生成,我直接在我的智能手機上運行它,但是當應用程序開始我得到以下日誌:無法實例化活動ComponentInfo .. java.lang.NullPointerException

06-24 16:35:35.653 1380-1380/com.rep E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.rep, PID: 1380 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rep/com.rep.app.principal.InicioActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2135) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
      at android.app.ActivityThread.access$800(ActivityThread.java:139) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5102) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:230) 
      at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43) 
      at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78) 
      at com.representemais.app.principal.InicioActivity.<init>(InicioActivity.java:86) 
      at java.lang.Class.newInstanceImpl(Native Method) 
      at java.lang.Class.newInstance(Class.java:1208) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1084) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2126) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
            at android.app.ActivityThread.access$800(ActivityThread.java:139) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5102) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
            at dalvik.system.NativeStart.main(Native Method) 

這就是InicioActivity:

package com.rep.app.principal; 

import android.os.AsyncTask; 
import android.os.Bundle; 

import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 

import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.android.volley.AuthFailureError; 
import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.Volley; 
import com.rep.R; 


import org.json.JSONObject; 

import java.util.HashMap; 

import java.util.Map; 


public class InicioActivity extends SherlockFragmentActivity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 



     AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask(); 
     mAutenticacaoLocalTask.execute((Void) null); 

    } 
    private TextView txtDisplay; 
    RequestQueue queue = Volley.newRequestQueue(this); 

    public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> { 

     @Override 
     protected Boolean doInBackground(Void... params) { 


      try { 




       txtDisplay = (TextView) findViewById(R.id.txtDisplay); 

       String url = "http://192.168.1.15/rep-api/api/clients"; 







       JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
         url, null, 
         new Response.Listener<JSONObject>() { 

          @Override 
          public void onResponse(JSONObject response) { 

           txtDisplay.setText("Response => "+response.toString()); 
           findViewById(R.id.progressBar1).setVisibility(View.GONE); 
          } 
         }, new Response.ErrorListener() { 



        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d("Error: " + error.getMessage()); 


        } 
       }) 



       { 

        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 

         HashMap<String, String> headers = new HashMap<String, String>(); 


         headers.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8"); 

         return headers; 
        } 



       }; 
       queue.add(jsonObjReq); 


       return true; 

      } catch (Exception e) { 
       Log.e("RM", e.getMessage()); 
       return false; 
      } 

     } 

     @Override 
     protected void onPostExecute(final Boolean success) { 

     } 

     @Override 
     protected void onCancelled() { 

     } 
    } 


} 

和清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.rep" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="19" /> 

    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 

     <activity 
      android:name=".app.principal.InicioActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".app.login.LoginActivity" 
      android:configChanges="keyboardHidden" 
      android:label="@string/app_name"> 
     </activity> 

     <activity 
      android:name=".app.principal.MainActivity" 
      android:label="@string/app_name"></activity> 

     <activity android:name=".app.cliente.ClienteDetalheActivity" 
      android:label="@string/app_name"></activity> 

     <activity android:name=".app.login.LoginTelaBloqueada" 
      android:label="@string/app_name"></activity> 
    </application> 



</manifest> 

回答

3

初始化您的RequestQueue,使用Volley.newRequestQueue(this);,在onCreate()之後super.onCreate()致電。您正試圖從初始化程序中使用您的Activity,這經常會導致這樣的問題。


第1步:替換:

RequestQueue queue = Volley.newRequestQueue(this); 

有:

RequestQueue queue = null; 

第2步:改變你的onCreate()是:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    queue=Volley.newRequestQueue(this); 

    AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask(); 
    mAutenticacaoLocalTask.execute((Void) null); 

} 
+0

,但現在隊列從未使用過 – AND4011002849

+0

@WARpoluido:如果你不是usin克它,擺脫它。 – CommonsWare

+0

但我在這一行上使用它:「queue.add(jsonObjReq);」 – AND4011002849

相關問題