一直试图弄清楚为什么我的代码不会在新的4.0模拟器中运行几天而且似乎无法触及它的底部,我的代码在2.2 sdk中运行得很好根本不会在4.0 sdk模拟器,由于这个我一直试图找到一些关于调试的教程,但似乎无法得到一个明确的解释,哪个是最好或坦率地说明如何执行函数,所以我的问题是哪个最好,最好的方法是什么?
更新
例外:
11-23 10:16:34.655: E/AndroidRuntime(1358): java.lang.RuntimeException:
Unable to resume activity {com.testapp2.second/com.testapp2.second.activities.AuthorizationActivity}: android.os.NetworkOnMainThreadException
11-23 10:16:34.655: E/AndroidRuntime(1358): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2443)
这是我的代码: 活性1:
package com.testapp2.second.activities;
import com.testapp2.second.OTweetApplication;
import com.testapp2.second.R;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
public class Testapp2Activity extends ListActivity {
private Twitter twitter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (OTweetApplication)getApplication();
twitter = app.getTwitter();
setContentView(R.layout.main);
}
@Override
protected void onResume() {
super.onResume();
if (!app.isAuthorized()) {
beginAuthorization();
} else {
loadTimelineIfNotLoaded();
}
}
private void loadTimelineIfNotLoaded() {
loadHomeTimeline();
}
private void beginAuthorization() {
Intent intent = new Intent(this, AuthorizationActivity.class);
startActivity(intent);
}
private void loadHomeTimeline() {
try {
ResponseList<Status> statii = twitter.getHomeTimeline();
StatusListAdapter adapter = new StatusListAdapter(this, statii);
setListAdapter(adapter);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class StatusListAdapter extends ArrayAdapter<Status> {
public StatusListAdapter(Context context, ResponseList<Status> statii) {
super(context, android.R.layout.simple_list_item_1, statii);
}
}
}
活动2:
package com.testapp2.second.activities;
import com.testapp2.second.OTweetApplication;
import com.testapp2.second.R;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AuthorizationActivity extends Activity {
private OTweetApplication app;
private WebView webView;
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
// the URL we're looking for looks like this:
// http://otweet.com/authenticated?oauth_token=1234567890qwertyuiop
Uri uri = Uri.parse(url);
if (uri.getHost().equals("otweet.com")) {
String token = uri.getQueryParameter("oauth_token");
if (null != token) {
webView.setVisibility(View.INVISIBLE);
app.authorized();
finish();
} else {
// tell user to try again
}
} else {
super.onLoadResource(view, url);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (OTweetApplication)getApplication();
setContentView(R.layout.authorization_view);
setUpViews();
}
@Override
protected void onResume() {
super.onResume();
String authURL = app.beginAuthorization();
webView.loadUrl(authURL);
}
private void setUpViews() {
webView = (WebView)findViewById(R.id.web_view);
webView.setWebViewClient(webViewClient);
}
}
扩展申请:
package com.testapp2.second;
import com.testapp2.second.authorization.OAuthHelper;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Application;
public class OTweetApplication extends Application {
private Twitter twitter;
private RequestToken currentRequestToken;
private OAuthHelper oAuthHelper;
@Override
public void onCreate() {
super.onCreate();
oAuthHelper = new OAuthHelper(this);
twitter = new TwitterFactory().getInstance();
oAuthHelper.configureOAuth(twitter);
}
public Twitter getTwitter() {
return twitter;
}
public boolean isAuthorized() {
return oAuthHelper.hasAccessToken();
}
public String beginAuthorization() {
try {
if (null == currentRequestToken) {
currentRequestToken = twitter.getOAuthRequestToken();
}
return currentRequestToken.getAuthorizationURL();
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
public boolean authorize(String pin) {
try {
AccessToken accessToken = twitter.getOAuthAccessToken(currentRequestToken, pin);
oAuthHelper.storeAccessToken(accessToken);
return true;
} catch (TwitterException e) {
throw new RuntimeException("Unable to authorize user", e);
}
}
public void authorized() {
try {
AccessToken accessToken = twitter.getOAuthAccessToken();
oAuthHelper.storeAccessToken(accessToken);
} catch (TwitterException e) {
throw new RuntimeException("Unable to authorize user", e);
}
}
}
oauth帮助者:
package com.testapp2.second.authorization;
import java.io.InputStream;
import java.util.Properties;
import com.testapp2.second.R;
import twitter4j.Twitter;
import twitter4j.auth.AccessToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class OAuthHelper {
private static final String APPLICATION_PREFERENCES = "app_prefs";
private static final String AUTH_KEY = "auth_key";
private static final String AUTH_SEKRET_KEY = "auth_secret_key";
private SharedPreferences prefs;
private AccessToken accessToken;
private String consumerSecretKey;
private String consumerKey;
private Context context;
public OAuthHelper(Context context) {
this.context = context;
prefs = context.getSharedPreferences(APPLICATION_PREFERENCES, Context.MODE_PRIVATE);
loadConsumerKeys();
accessToken = loadAccessToken();
}
public void configureOAuth(Twitter twitter) {
twitter.setOAuthConsumer(consumerKey, consumerSecretKey);
twitter.setOAuthAccessToken(accessToken);
}
public boolean hasAccessToken() {
return null != accessToken;
}
public void storeAccessToken(AccessToken accessToken) {
Editor editor = prefs.edit();
editor.putString(AUTH_KEY, accessToken.getToken());
editor.putString(AUTH_SEKRET_KEY, accessToken.getTokenSecret());
editor.commit();
this.accessToken = accessToken;
}
private AccessToken loadAccessToken() {
String token = prefs.getString(AUTH_KEY, null);
String tokenSecret = prefs.getString(AUTH_SEKRET_KEY, null);
if (null != token && null != tokenSecret) {
return new AccessToken(token, tokenSecret);
} else {
return null;
}
}
private void loadConsumerKeys() {
try {
Properties props = new Properties();
InputStream stream = context.getResources().openRawResource(R.raw.oauth);
props.load(stream);
consumerKey = (String)props.get("consumer_key");
consumerSecretKey = (String)props.get("consumer_secret_key");
} catch (Exception e) {
throw new RuntimeException("Unable to load consumer keys from oauth.properties", e);
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp2.second"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light"
android:name=".OTweetApplication" >
<activity
android:label="@string/app_name"
android:name=".activities.Testapp2Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.AuthorizationActivity" android:label="@string/authorization" />
</application>
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
答案 0 :(得分:1)
第一个例外是:NetworkOnMainThreadException
你永远不应该在UI线程上做网络内容。你应该在Thread / AsyncTask中完成它。
如果您向我们提供了一些活动代码,我们可以告诉您应该更改的内容。
更新
在onResume()
AuthorizationActivity
中,您拨打app.beginAuthorization();
,可以拨打twitter.getOAuthRequestToken()
,就我在twitter4j api中看到的那样,建立网络连接。所以这不应该在UI线程/主线程中运行的onResume()
中完成。
我的建议:在AsyncTask
中进行Twitter通信/身份验证,因为您永远不知道连接速度有多快/慢。您应该在连接速度较慢时测试当前的应用程序。我想你的应用程序和/或application not responding
消息的响应可能会很慢。
阅读本文以获取更多信息:Designing for Responsiveness
它在4.0以下工作的原因是:它在API 11中引入:NetworkOnMainThreadException
答案 1 :(得分:0)
Imo ddms,在那里你可以看到logcat,它会告诉你所有你需要知道你的应用程序发生了什么。我不确定r15,主要是因为我正在使用设备进行调试,但在旧版本上,仿真器无法获得所有异常,使调试阶段变得痛苦
答案 2 :(得分:0)
DDMS不是源代码调试器,它提供:
另一方面,使用Eclipse,您可以在代码中设置断点。