2015-12-06 144 views
1

我想從我的Android項目的訓練做一個迷你yamba.newcircle.com。當互聯網不可用時,我正在保存文本信息,如果互聯網啓動,我正在保存連接廣播接收機。這個應用程序自動啓動一個服務,該服務循環將ActiveAndroid中所有保存的文本行從表中通過HTTP帖子推送到Web API。無法實例化服務顯示java.lang.NullPointerException

當我打開WiFi關閉,保存ActiveAndroid一些消息,並打開WiFi回來;如果我的應用程序連接,它只會崩潰NullPointereException

有什麼建議嗎?

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="pt.flag.miniyamba" > 

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


    <application 
     android:name="MiniYamba" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <meta-data 
      android:name="AA_DB_NAME" 
      android:value="yamba.db" /> 
     <meta-data 
      android:name="AA_DB_VERSION" 
      android:value="5" /> 

     <activity android:name=".Main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".OnlineStatus" > 
     </activity> 
     <activity android:name=".OfflineStatus" > 
     </activity> 
     <activity android:name=".SingleStatus" > 
     </activity> 
     <activity android:name=".NewStatus" > 
     </activity> 

     <receiver android:name=".NetworkBroadCastReceiver"> 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action> 
      </intent-filter> 
     </receiver> 
     <service android:name=".PushOfflineSavedService"/> 

    </application> 

</manifest> 

broacast

public class NetworkBroadCastReceiver extends BroadcastReceiver { 

    private Context mContext; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     ConnectivityManager manager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = manager.getActiveNetworkInfo(); 

     if(netInfo != null && netInfo.isConnectedOrConnecting()){ 
      Toast.makeText(context, "net is up, pushing our previous offline saved posts ", Toast.LENGTH_SHORT).show(); 

      //inicializar o servico de push de posts, guardados anteriormente 

      Intent novointent = new Intent(context, PushOfflineSavedService.class); 
      context.startService(novointent); 
     } 
     //mudou de conectividade, mas nao tem internet 
     else{ 
      Toast.makeText(context, "Connectivity is changed. Internet seens not working.", Toast.LENGTH_SHORT).show(); 
     } 

    } 



} 

intentservice

public class PushOfflineSavedService extends IntentService { 

    private String LOG_TAG; 

    public PushOfflineSavedService() { 

     super("PushOfflineSavedService"); 
     ActiveAndroid.initialize(this); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     //enviar os status guardados, por enviar 

     List<OfflinePostToSend> lista = new Select().from(OfflinePostToSend.class).execute(); 

     //loop para enviar os status 
     if (lista.size() > 0) { 

      for (int i = 0; i < lista.size(); i++) { 

       HttpURLConnection urlConnection = null; 
       BufferedReader reader = null; 

       try { 
        String path = "http://yamba.newcircle.com/api/statuses/update.json"; 
        String status = lista.get(i).getText(); 

        URL url = new URL(path); 
        String userPass = "student:password"; 
        String token = "Basic " + Base64.encodeToString(userPass.getBytes(), Base64.NO_WRAP); 
        urlConnection = (HttpURLConnection) url.openConnection(); 
        urlConnection.setRequestMethod("POST"); 
        // Adicionar o token como header do pedido 
        urlConnection.addRequestProperty("Authorization", token); 
        urlConnection.setDoOutput(true); 

        String postParameters = "status="+status; 
        urlConnection.setFixedLengthStreamingMode(postParameters.getBytes().length); 
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 

        // Adicionar o status ao método post 
        PrintWriter out = new PrintWriter(urlConnection.getOutputStream()); 
        out.print(postParameters); 
        out.close(); 

        reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(urlConnection.getInputStream()))); 
        StringBuilder buffer = new StringBuilder(); 

        String line; 
        while ((line = reader.readLine()) != null) { 
         buffer.append(line); 
         buffer.append("\n"); 
        } 
        //se buffer nao for full, temos 1 respota do API 
        if (buffer.length() != 0) { 
         // Stream was NOT empty. 
         //apagar este elemento da lista e da db 

         lista.get(i).delete(); 

        } 

        //return buffer.toString(); 

       } catch (IOException e) { 
        Log.e(LOG_TAG, "Error ", e); 
       } finally { 
        if (urlConnection != null) { 
         urlConnection.disconnect(); 
        } 
        if (reader != null) { 
         try { 
          reader.close(); 
         } catch (final IOException e) { 
          Log.e(LOG_TAG, "Error closing stream", e); 
         } 
        } 
       } 
      } 
     } 

    } 
} 
+0

哪裏是logcat? –

+0

發佈您的logcat。 – Pankaj

+0

對不起,編輯追加登錄 –

回答

0

ActiveAndroid guide示出了在一個子類Application調用initialize(),像這樣:

public class MyApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     ActiveAndroid.initialize(this); 
    } 
} 

嘗試擴展應用程序並將您的initialize()呼叫而不是在服務中。

+0

公共類MiniYamba延伸申請{ @Override 公共無效的onCreate(){ super.onCreate(); ActiveAndroid.initialize(this); }} 和 –

+0

清單 機器人:名字= 「MiniYamba」 –

+0

難道這些變化解決這一問題? –

相關問題