由于致命异常,应用程序无法正常工作

时间:2012-08-17 05:48:22

标签: android dropbox

我使用Froyo版本来运行应用程序。我在运行项目时遇到了致命异常,但应用程序意外关闭了。我在activity_main.xml文件中有3个按钮。

主要活动代码

 public class Main extends Activity {
private DropboxAPI<AndroidAuthSession> mDBApi;
final static String APP_KEY = "707tr8wwfpqrl7z";
final static String APP_SECRET = "mhyp3cxb8eeiyb6";
final static AccessType ACCESS_TYPE = AccessType.APP_FOLDER;

SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String dropbox_key = prefs.getString("dropbox_key", "");
    String dropbox_secret = prefs.getString("dropbox_secret", "");


    if (dropbox_key.length() > 0 && dropbox_secret.length() > 0) {
        AccessTokenPair access = new AccessTokenPair(dropbox_key, dropbox_secret);
        AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
        session.setAccessTokenPair(access);
        mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    }

    Button link = (Button) findViewById(R.id.button1);
    Button upload = (Button) findViewById(R.id.button2);
    Button download = (Button) findViewById(R.id.button3);


    link.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
            AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
            if (mDBApi == null) {
                mDBApi = new DropboxAPI<AndroidAuthSession>(session);
            }
            mDBApi.getSession().startAuthentication(Main.this);
        }
    });

    upload.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            File dir = new File(getFilesDir().getAbsolutePath());
            try {
                PrintWriter out = new PrintWriter(new FileWriter(dir + "/test.txt"));
                for (int i = 0; i < 20; i++) {
                    out.println("omg");
                }
                out.close();
                File file = new File(getFilesDir().getAbsolutePath(), "/test.txt");
                FileInputStream in = new FileInputStream(file);
                mDBApi.putFileOverwrite("/test.txt", in, file.length(), null);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DropboxException e) {
                e.printStackTrace();
            }
        }
    });

    download.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                File output = new File("/mnt/sdcard/test.txt");
                OutputStream out = new FileOutputStream(output);
                mDBApi.getFile("/test.txt", null, out, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DropboxException e) {
                e.printStackTrace();
            }
        }
    });
}


@Override
protected void onResume() {
    super.onResume();
    if (mDBApi != null && mDBApi.getSession().authenticationSuccessful()) {
        try {
            mDBApi.getSession().finishAuthentication();
            AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();
            Editor editor = prefs.edit();
            editor.putString("dropbox_key", tokens.key);
            editor.putString("dropbox_secret", tokens.secret);
            editor.commit();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
  }
}

清单代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dropboxdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application 
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity 
        android:name=".Main"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <data android:scheme="db-707tr8wwfpqrl7z" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.dropbox.client2.android.AuthActivity"
        android:configChanges="orientation|keyboard"
        android:launchMode="singleTask" >
        <intent-filter>
            <data android:scheme="db-707tr8wwfpqrl7z" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>
</manifest>

LogCat错误

 08-17 10:47:43.935: E/AndroidRuntime(346): FATAL EXCEPTION: main
 08-17 10:47:43.935: E/AndroidRuntime(346): java.lang.ExceptionInInitializerError
 08-17 10:47:43.935: E/AndroidRuntime(346): atjava.lang.Class.newInstanceImpl(Native Method)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at java.lang.Class.newInstance(Class.java:1409)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 10:47:43.935: E/AndroidRuntime(346):  at android.os.Looper.loop(Looper.java:123)
 08-17 10:47:43.935: E/AndroidRuntime(346):     at android.app.ActivityThread.main(ActivityThread.java:3683)

清除此错误的任何解决方案?

2 个答案:

答案 0 :(得分:1)

Minsdk版本是10,即姜饼。

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

你需要为Froyo设置为8

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

答案 1 :(得分:0)

将minSdkVersion更改为8而不是10。

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />