[英]How to debug this Android Studio app
我嘗試使用第二個Android應用程序(在Android Studio中)訪問網絡資源。 我在API 15上的最小,目標和構建目標。 這是我的課。 它根本不是MVC,我只是將在線開發您的第一個Android App教程中的所有內容投入了MainActivity類:
public class MainActivity extends Activity {
private static final String DEBUG_TAG = "HttpExample";
private static final String MYURL = "http://www.server.com/app/service.php";
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.hello_world);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//Check connectivity
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
//fetch data
new DownloadWebpageTask().execute(MYURL);
textView.setText("Fetching data");
} else {
//show error
textView.setText("No network connection available.");
}
}
//Method to connect to the internet
// Uses AsyncTask to create a task away from the main UI thread. This task takes a
// URL string and uses it to create an HttpUrlConnection. Once the connection
// has been established, the AsyncTask downloads the contents of the webpage as
// an InputStream. Finally, the InputStream is converted into a string, which is
// displayed in the UI by the AsyncTask's onPostExecute method.
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
//Method to convert url to url object
// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
//Convert input stream to string
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
這是巨大的日志文件:
好吧,我不會發布它,但是我有兩個問題:
如何減少log cat中的所有輸出。 我已經從冗長的調試切換到了調試,但仍然有很多東西。 在這種情況下,由於應用程序崩潰,我沒有時間去做另一個SO帖子所說的選擇正在運行的進程,然后點擊2個綠色箭頭按鈕,該按鈕僅過濾出正在運行的進程的輸出。 在篩選出日志以便我可以查看導致它崩潰的原因之前,我是否需要使該應用程序不崩潰? 否則很難對所有日志進行排序。 最佳做法是什么?
我確實得到了我的日志之一; “ 01-30 10:40:58.665 2047-2072 / com.santiapps.downloadwebdata D / HttpExample:響應為:200”,它在代碼中,但隨后幾行卻導致崩潰:
01-30 10:40:58.710 2047-2047 / com.santiapps.downloadwebdata E / AndroidRuntime致命異常:com.santiapps.downloadwebdata.MainActivity $ DownloadWebpageTask.onPostExecute(MainActivity.java:77)處為main java.lang.NullPointerException com.santiapps.downloadwebdata.MainActivity $ DownloadWebpageTask.onPostExecute(MainActivity.java:63)在android.os.AsyncTask.finish(AsyncTask.java:631)在android.os.AsyncTask.access $ 600(AsyncTask.java:177)在android.os.Looper.loop(Looper.java:137)處的android.os.Handler.dispatchMessage(Handler.java:99)處的android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) .ActivityThread.main(ActivityThread.java:5071)(位於java.lang.reflect.Method.invokeNative(本機方法)(位於java.lang.reflect.Method.invoke(Method.java:511))(位於com.android.internal.os) .ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:808)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)在dalvik.system.NativeStart.main(本機方法)01-30 10:40 :58.717 83 7-2676 /? W / ActivityManager:強制完成活動com.santiapps.downloadwebdata / .MainActivity
我看到的是一個空指針異常。 我該如何遵循? 抱歉,我是android新手。 謝謝
您可以通過單擊鏈接來關注logcat,例如MainActivity.java:77 。
您可以通過創建一個新的過濾器來過濾日志,(見圖)
您可以在其中按Package Name進行過濾,因此只有您的應用程序日志可見
您的TextView為null ...
通常,您可以這樣初始化它:
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.id_of_your_textview);
至於減少日志量,您應該在應用程序的程序包名稱上過濾日志。 在Android Studio中,它應該位於Android DDMS面板中“日志級別”的右側(目前可能顯示為“無過濾器”,您需要通過單擊“編輯過濾器配置”來創建一個)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.