android 调试 ddms 与 eclipse 调试器/代码在 android 4.0 上不起作用

发布于 2024-12-17 13:41:54 字数 8364 浏览 3 评论 0原文

几天来我一直试图弄清楚为什么我的代码无法在新的 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 helper:

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>

been trying to figure out why my code wont run in the new 4.0 emulator for a few days now and cant seem to get to the bottom of it, My code which runs just fine in the 2.2 sdk wont work at all in the 4.0 sdk emulator, as a result of this Ive been trying to track down some tutorials on debugging but cant quite seem to get a clear explanation as to which one is best or quite frankly a clear way as to how to execute the functions, so my question is which is best and what is the best way to do it?

Update:

Exceptions:

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)

here is my code:
activity1:

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);
}

}
}

activity 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);
}

}

extended application:

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 helper:

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);
}
}
}

manifest:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

用心笑 2024-12-24 13:41:54

第一个异常说明了一切: NetworkOnMainThreadException

您永远不应该在 UI 线程上执行网络操作。您应该在线程/异步任务中执行此操作。

如果您向我们提供您的活动的一些代码,我们可以告诉您应该更改哪些内容。

更新:

AuthorizationActivityonResume() 中,您调用 app.beginAuthorization(); ,它可以调用 twitter.getOAuthRequestToken()< /code> 据我在 twitter4j api 中看到的,它可以建立网络连接。因此,这不应该在 UI 线程/主线程中运行的 onResume() 中完成。

我的建议:在 AsyncTask 中进行 twitter 通信/身份验证,因为您永远不知道连接可能有多快/慢。您应该在连接速度较慢时测试当前的应用程序。我想您可能会收到应用程序响应缓慢和/或应用程序未响应消息。

阅读此内容以获取更多信息:响应式设计

它在 4.0 以下运行的原因是:它是在 API 11 中引入的: NetworkOnMainThreadException

The first exception says everything: NetworkOnMainThreadException

You should never ever do network stuff on the UI thread. You should do it in a Thread/AsyncTask.

If you provide us some code of your activity, we can say you what you should change.

Update:

In your onResume() of AuthorizationActivity you call app.beginAuthorization(); which can call twitter.getOAuthRequestToken() which, as far as I saw in the twitter4j api, makes network connections. So this shouldn't be done in onResume() which runs in the UI Thread/Main Thread.

My advice: Make the twitter communication/authentication in an AsyncTask as you never know how fast/slow a connection might be. You should test your current application while having a slow connection. I guess you might get slow response of your application and/or an application not responding message.

Read this for further information: Designing for Responsiveness

The reason it worked below 4.0 was: it was introduced in API 11: NetworkOnMainThreadException

淑女气质 2024-12-24 13:41:54

在 ddms 中,您可以看到 logcat,它会告诉您有关应用程序发生的情况所需的所有信息。我不确定 r15,主要是因为我一直在使用设备进行调试,但在旧版本上,模拟器无法获取所有异常,使得调试阶段变得痛苦

Imo ddms, there you can see logcat that will tell you all you need to know about what is happening to your app. I'm not sure about r15, mainly because i have being using a device to debug, but on older versions the emulator couldn't get all exceptions, making the debug phase painful

思念绕指尖 2024-12-24 13:41:54

DDMS 不是源代码调试器,它提供:

  • 端口转发服务 设备
  • 上的屏幕捕获 设备
  • 上的线程和堆信息
  • logcat、进程和无线电状态信息
  • 来电和 SMS 欺骗
  • 位置数据欺骗

另一方面,使用 Eclipse您可以在代码中设置断点。

DDMS is not a source code debugger, it provides:

  • port-forwarding services
  • screen capture on the device
  • thread and heap information on the device
  • logcat, process, and radio state information
  • incoming call and SMS spoofing
  • location data spoofing

On the other hand, with Eclipse you can set breakpoints in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文