2016-03-15 55 views
0

我在Android中從container獲得空值。無法在Android中獲取container.addView()

@Bind(R.id.container) 
ViewGroup container; 

private void addUploadToList(String uploadID, String filename) { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View uploadProgressView = inflater.inflate(R.layout.view_upload_progress, null); 
     UploadProgressViewHolder viewHolder = new UploadProgressViewHolder(uploadProgressView, filename); 
     viewHolder.uploadId = uploadID; 
     container.addView(viewHolder.itemView, 0); 
     uploadProgressHolders.put(uploadID, viewHolder); 
} 

我不能讓值container = null,例外的是:

System.err: java.lang.NullPointerException 
System.err:  at kr.co.composer.callrecord.recorder.CallRecordService.addUploadToList(CallRecordService.java:164) 

這個模塊,我從工作完美的民俗。

https://github.com/gotev/android-upload-service/tree/master/examples/app

我想用onMultipartUploadClick()這種融入我的項目將文件保存時要成功上傳。

線後:

recorder.stop();

這個文件在我的項目: http://pastebin.com/KATBMTCe

此文件extends Servicein Github responsitoryAppCompatActivity延伸。

這是所有問題,我嘗試使用此GitHub中的代碼responsitory來自動上傳何時調用結束。

+0

您傳遞給addUploadToList的對象是否已正確初始化? – Nick

+0

您尚未初始化變量「容器」。 –

+0

如果是片段 – Sanny

回答

0

變量container未初始化。與初始化,

WindowManager container = (WindowManager) getSystemService(Context.WINDOW_SERVICE) 

所以你的代碼會變得,

private void addUploadToList(String uploadID, String filename) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    WindowManager container = (WindowManager) getSystemService(WINDOW_SERVICE); 
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
     //WindowManager.LayoutParams.TYPE_INPUT_METHOD | 
     WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
     WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 
     PixelFormat.TRANSLUCENT); 
    params.gravity = Gravity.RIGHT | Gravity.TOP; 

    View uploadProgressView = inflater.inflate(R.layout.traslucent, null); 
    UploadProgressViewHolder viewHolder = new UploadProgressViewHolder(uploadProgressView, filename);  
    viewHolder.uploadId = uploadID; 
    uploadProgressHolders.put(uploadID, viewHolder); 
    container.addView(viewHolder.itemView, params); 
} 
+0

我遇到錯誤:無法找到符號方法getLayoutParams() – lask

+0

謝謝@K Neeraj Lal。我嘗試這個代碼,它運行正常,但似乎無法正常工作。 – lask

0

您正在使用Butterknife。你需要在活動/片段中注入視圖。

活動

@Override public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.simple_activity); 
    ButterKnife.bind(this); 
    // TODO Use fields... 
} 

片段

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fancy_fragment, container, false); 
    ButterKnife.bind(this, view); 
    // TODO Use fields... 
    return view; 
} 
+0

是的,我使用Butterknife,但我不能像你寫的那樣改變代碼。 – lask

+0

我不是爲什麼你不能這樣做! –

0

如果你想使用的服務,使用LocalBroadcastManager自定義事件,從Activity聽它,做下載工作中Activity作爲MultipartUploadRequestAsync

CallRecordService.java

try { 
     recorder.stop(); 
     //emit event 
     LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("download-event")); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我相信你有活性的啓動service.Put低於烏爾活動代碼

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


     //register and listen event 
     LocalBroadcastManager.getInstance(this).registerReceiver(new DownloadReceiver(), new IntentFilter("download-event")); 

    } 


    private class DownloadReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // call multipart download function here 
     } 
    } 

有了這樣至少你在活動注入佈局通常情況下,可以清楚知道爲什麼containernull。確保你的xml佈局文件中有正確的id。