应用程序在模拟器中工作但在手机上崩溃

时间:2015-12-24 19:19:43

标签: android android-studio twitter

成功制作应用程序,但在手机上运行时给我错误

在android studio

中存在的模拟器上正常工作

主要活动:

private static final String TWITTER_KEY = "smth";
private static final String TWITTER_SECRET = "smth";
private TwitterLoginButton loginButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new Twitter(authConfig));
    setContentView(R.layout.activity_main);

    loginButton =(TwitterLoginButton) findViewById(R.id.twitter_login_button);

    loginButton.setCallback(new Callback<TwitterSession>()

                            {
                                @Override
                                public void success(Result<TwitterSession> result) {
                                    TwitterSession session= result.data;
                                    String user=session.getUserName();
                                    Intent tl=new Intent(MainActivity.this, TweetListActivity.class);
                                    Bundle b= new Bundle();
                                    b.putString("username",user);
                                    tl.putExtras(b);
                                    startActivity(tl);
                                }

                            @Override
                            public void failure(TwitterException exception) {
                                Log.d("TwitterKit", "Login with Twitter failure", exception);
                            }
                        }

);
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
StatusesService statusesService = twitterApiClient.getStatusesService();
statusesService.show(524971209851543553L, null, null, null, new Callback<Tweet>() {
    @Override
    public void success(Result<Tweet> result) {

    }

    public void failure(TwitterException exception) {
        //Do something on failure


     }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    loginButton.onActivityResult(requestCode, resultCode, data);
}

TweetList活动:

String user;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timeline);
    Intent tl=getIntent();
    Bundle b =tl.getExtras();

    if(null != b){
        user = b.getString("username");
    }
    final UserTimeline userTimeline = new UserTimeline.Builder()
            .screenName(user)
            .build();
    final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
            .setTimeline(userTimeline)
            .build();
    setListAdapter(adapter);
}

活动主要:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.rohil.aurus.MainActivity">


    <com.twitter.sdk.android.core.identity.TwitterLoginButton
        android:id="@+id/twitter_login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>



</LinearLayout>

tweet_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:text="empty"/>

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>
</LinearLayout>

在模拟器上工作正常但在手机上崩溃 Galaxy S4- 1920x1080p Android 5.1.1 使应用程序达到最低sdk 21

logcat的:

 D/AndroidRuntime(29639): Shutting down VM
--------- beginning of crash
E/AndroidRuntime(29639): FATAL EXCEPTION: main
E/AndroidRuntime(29639): Process: com.rohil.aurus, PID: 29639
E/AndroidRuntime(29639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rohil.aurus/com.rohil.aurus.MainActivity}: java.lang.IllegalStateException: Must have valid session. Did you authenticate with Twitter?
E/AndroidRuntime(29639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
E/AndroidRuntime(29639):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
E/AndroidRuntime(29639):    at android.app.ActivityThread.access$900(ActivityThread.java:154)
E/AndroidRuntime(29639):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
E/AndroidRuntime(29639):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(29639):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(29639):    at android.app.ActivityThread.main(ActivityThread.java:5294)
E/AndroidRuntime(29639):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(29639):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(29639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime(29639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime(29639):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
E/AndroidRuntime(29639): Caused by: java.lang.IllegalStateException: Must have valid session. Did you authenticate with Twitter?
E/AndroidRuntime(29639):    at com.twitter.sdk.android.core.TwitterCore.getApiClient(TwitterCore.java:259)
E/AndroidRuntime(29639):    at com.rohil.aurus.MainActivity.onCreate(MainActivity.java:62)
E/AndroidRuntime(29639):    at android.app.Activity.performCreate(Activity.java:5990)
E/AndroidRuntime(29639):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
E/AndroidRuntime(29639):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
E/AndroidRuntime(29639):    ... 11 more
W/ActivityManager( 3214):   Force finishing activity 1 com.rohil.aurus/

https://drive.google.com/file/d/0Byu333mD4mcSYi01X2N1Sjg3X1k/view?usp=docslist_api

1 个答案:

答案 0 :(得分:0)

Twitter API需要身份验证。使用您的模拟器,您可能在开发期间的某个时刻执行此操作。由于身份验证密钥仍然存在,您可能已进行更改以超越此步骤,现在,当您在新设备上安装时,未发生身份验证,因此您会收到此错误。

如果您检查代码或查看Twitter的文档,您将在此处找到绕过此步骤的位置。 Twitter的Oauth文档在这里:https://dev.twitter.com/oauth