2016-11-08 58 views
0

我試圖爲P4執行連接測試,但是我reciing一個「空指針異常錯誤」爲P4空指針異常運行連接測試Android Studio中

錯誤消息:

:00:02 PM null 
java.lang.NullPointerException 
at com.android.ddmlib.Client.read(Client.java:692) 
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:304) 
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:256) 

它是一個標準的測試,在異步任務

測試功能驗證非空字符串:

public void runCloudModuleTest() { 
    String joke = null; 
    JokesAsyncTask jokesAsyncTask = new JokesAsyncTask(getContext(), null); 
    jokesAsyncTask.execute(); 
    try { 
     joke = jokesAsyncTask.get(); 
     Log.d("CloudModuleTest", "Retrieved a non-empty string successfully: " + joke); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    assertNotNull(joke); 
} 

有人可以幫我理解問題是什麼嗎?

的AsyncTask:該異步任務從谷歌雲引擎

公共類JokesAsyncTask延伸的AsyncTask,空隙,字符串> {

private static JokeApi myApiService = null; 
private Context mContext; 
private String mResult; 
private ProgressBar mProgressBar; 

public JokesAsyncTask(Context context, ProgressBar progressBar) { 
    this.mContext = context; 
    this.mProgressBar = progressBar; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    if (mProgressBar != null) { 
     mProgressBar.setVisibility(View.VISIBLE); 
    } 
} 

@Override 
protected String doInBackground(Pair<Context, String>... pairs) { 
    if (myApiService == null) { 
     JokeApi.Builder builder = new JokeApi.Builder(AndroidHttp.newCompatibleTransport(), 
       new AndroidJsonFactory(), null) 
       .setRootUrl("https://testandroiddevelopment.appspot.com/_ah/api/"); 
     myApiService = builder.build(); 
    } 
    try { 
     return myApiService.sendJoke(new JokeBean()).execute().getJoke(); 
    } catch (IOException e) { 
     return e.getMessage(); 
    } 
} 

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    if (mProgressBar != null) { 
     mProgressBar.setVisibility(View.GONE); 
    } 
    mResult = result; 
    startJokeDisplayActivity(); 
} 

private void startJokeDisplayActivity() { 

    Intent intent = new Intent(mContext, JokeViewActivity.class); 
    intent.putExtra(JokeViewActivity.JOKE_KEY, mResult); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    mContext.startActivity(intent); 
} 

}

提取數據我已經引用的變量,它是不是由於下面的帖子所致的問題,但是我確實進行了調查並最終清理並重建了幫助解決問題的項目

+0

在異常之前打印「null」是什麼?看來你的AsyncTask返回null,所以顯示你的'JokesAsyncTask'類 –

+0

添加了Asynctask類 –

+0

'myApiService'是OkHttp還是Retrofit?爲什麼你需要一個AsyncTask? –

回答

0

我已經引用了變量,並且它不是由於post @AxelH引起的問題,但是我確實調查並最終清理並重建了幫助解決問題的項目