如何从其他图书馆活动中调用一个图书馆活动?

时间:2012-09-07 10:50:11

标签: android android-manifest android-library

我没有任何其他方式可以提出这个问题,所以这是我的问题: 我有一个库项目,我用于两个应用程序,它运作良好。现在我有两个应用程序的通用登录页面和登录功能,并避免重复我想要为这两个应用程序使用相同的登录活动的代码。这是我使用的代码

图书馆项目

     public class CanteenPreference {

        // Static variables declaration
        private static final String KEY_INSTANCE_URL = "instance_url";
        private static final String KEY_USER_NAME = "user_name_preferences";
        private static final String KEY_ACCESS_KEY = "access_key_preferences";
        public static final String DEFAULT_URL = "some url";
        private static final String URL_SUFFIX = "/api/call";

        /**
         * Gets the preferred url.
         * 
         * @param activity
         * @return
         */
        public static String getUrl(Activity activity){
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
            String previousURL = sharedPrefs.getString(KEY_INSTANCE_URL, null);
            if (previousURL == null) {
                Intent in=new Intent(activity,LoginActivity.class);
                activity.startActivity(in);

                previousURL = sharedPrefs.getString(KEY_INSTANCE_URL, DEFAULT_URL + URL_SUFFIX);
            }
            return previousURL;
        }

        /**
         * Gets the preferred user name.
         * 
         * @param activity
         * @return
         */
        public static String getUserName(Activity activity){
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
            String previousUsername = sharedPrefs.getString(KEY_USER_NAME, null);
            if (previousUsername == null) {
                activity.startActivity(new Intent(activity, LoginActivity.class));

                previousUsername = sharedPrefs.getString(KEY_USER_NAME, "test");
            }
            return previousUsername;
        }

        /**
         * Gets the preferred password.
         * 
         * @param activity
         * @return
         */
        public static String getPassword(Activity activity){
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
            String previousPassword = sharedPrefs.getString(KEY_ACCESS_KEY, null);
            if (previousPassword == null) {
                activity.startActivity(new Intent(activity, LoginActivity.class));

                previousPassword = sharedPrefs.getString(KEY_ACCESS_KEY, "test");
            }
            return previousPassword;
        }

        /**
         * Writes the preferences
         * 
         * @param activity
         * @param url
         * @param userName
         * @param password
         * @return true if committed
         */
        public static boolean write(Activity activity, String url, String userName, String password){
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
            SharedPreferences.Editor prefEditor = sharedPrefs.edit();

            prefEditor.putString(KEY_INSTANCE_URL, url + URL_SUFFIX);
            prefEditor.putString(KEY_USER_NAME, userName);
            prefEditor.putString(KEY_ACCESS_KEY, password);
            return prefEditor.commit();
        }
    }

和库中的loginactivity

 public class LoginActivity extends Activity {
    private EditText mUrlInput;
    private EditText mUserInput;
    private EditText mPasswordInput;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        mButton = (Button) findViewById(R.id.loginButt);
        mUrlInput = (EditText) findViewById(R.id.urlInput);
        mUserInput = (EditText) findViewById(R.id.userInput);
        mPasswordInput = (EditText) findViewById(R.id.passwordInput);

        mUrlInput.setText(CanteenPreference.DEFAULT_URL);

        mButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                final String url = mUrlInput.getText().toString();
                final String userName = mUserInput.getText().toString();
                final String password = mPasswordInput.getText().toString();

                if (url == null || url.length() == 0 || userName == null
                        || userName.length() == 0 || password == null
                        || password.length() == 0) {
                    Toast toast = Toast.makeText(LoginActivity.this,
                            "All fields are required", Toast.LENGTH_SHORT);
                    toast.show();

                } else {

                    Thread loginThread = new Thread() {
                        @Override
                        public void run() {
                            try {
                                DefaultHttpClient httpClient = new DefaultHttpClient();
                                HttpPost httpPost = new HttpPost(url);
                                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                                nameValuePairs.add(new BasicNameValuePair(
                                        "username", userName));
                                nameValuePairs.add(new BasicNameValuePair(
                                        "password", password));

                                httpPost.setEntity(new UrlEncodedFormEntity(
                                        nameValuePairs));
                                HttpResponse httpResponse = httpClient
                                        .execute(httpPost);

                                if (httpResponse.getStatusLine()
                                        .getStatusCode() != HttpStatus.SC_OK) {
                                    throw new Exception("Invalid credentials !");
                                }

                            } catch (Exception e) {
                                Log.d("Login: ", "Invalid credentials", e);
                                Toast toast = Toast.makeText(
                                        LoginActivity.this,
                                        "Invalid credentials",
                                        Toast.LENGTH_SHORT);
                                toast.show();
                            }
                            // write the preferences
                            CanteenPreference.write(LoginActivity.this, url,
                                    userName, password);
                            // finish the activity
                            LoginActivity.this.finish();
                        }
                    };

                    loginThread.start();
                }
            }
        });
    }
}

我已经在应用程序和库清单中定义了Login活动,但仍然遵循logcat`

    09-07 16:00:12.436: W/ActivityThread(1429): Application com.jumbybay.ferry is waiting for the debugger on port 8100...
09-07 16:00:12.465: I/System.out(1429): Sending WAIT chunk
09-07 16:00:12.465: I/dalvikvm(1429): Debugger is active
09-07 16:00:12.476: I/System.out(1429): Debugger has connected
09-07 16:00:12.476: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:12.685: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:12.836: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:12.896: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:12.896: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:13.096: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:13.306: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:13.336: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:13.346: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:13.505: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:13.708: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:13.836: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:13.856: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:13.906: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:14.106: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:14.306: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:14.376: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:14.385: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:14.515: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:14.717: I/System.out(1429): waiting for debugger to settle...
09-07 16:00:14.846: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:14.866: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:14.916: I/System.out(1429): debugger has settled (1325)
09-07 16:00:15.356: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:15.375: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:15.866: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:15.886: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:15.886: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:16.375: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:16.385: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:16.385: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:16.866: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:16.876: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:16.886: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:17.375: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:17.385: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:17.385: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:17.886: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:17.896: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:17.896: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:18.385: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:18.395: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:18.395: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:18.896: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:18.906: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:18.906: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:19.395: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:19.405: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:19.405: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:19.896: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:19.916: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:19.916: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:20.395: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:20.415: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:20.415: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:20.906: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:20.926: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:20.926: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:21.405: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:21.425: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:21.425: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:00:21.916: I/dalvikvm(1429): threadid=3: reacting to signal 3
09-07 16:00:21.926: D/dalvikvm(1429): threadid=1: still suspended after undo (sc=1 dc=1)
09-07 16:00:21.936: I/dalvikvm(1429): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:01:15.126: W/ActivityThread(1470): Application com.jumbybay.ferry is waiting for the debugger on port 8100...
09-07 16:01:15.176: I/System.out(1470): Sending WAIT chunk
09-07 16:01:15.176: I/dalvikvm(1470): Debugger is active
09-07 16:01:15.385: I/System.out(1470): Debugger has connected
09-07 16:01:15.385: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:15.585: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:15.787: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:15.996: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:16.195: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:16.399: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:16.601: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:16.795: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:16.996: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:17.207: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:17.408: I/System.out(1470): waiting for debugger to settle...
09-07 16:01:17.610: I/System.out(1470): debugger has settled (1403)
09-07 16:02:10.908: D/AndroidRuntime(1470): Shutting down VM
09-07 16:02:10.908: W/dalvikvm(1470): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-07 16:02:11.056: E/AndroidRuntime(1470): FATAL EXCEPTION: main
09-07 16:02:11.056: E/AndroidRuntime(1470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jumbybay.ferry/com.jumbybay.ferry.FerryMain}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jumbybay.ferry/com.jumbybay.activity.LoginActivity}; have you declared this activity in your AndroidManifest.xml?
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.os.Looper.loop(Looper.java:137)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at java.lang.reflect.Method.invoke(Method.java:511)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at dalvik.system.NativeStart.main(Native Method)
09-07 16:02:11.056: E/AndroidRuntime(1470): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jumbybay.ferry/com.jumbybay.activity.LoginActivity}; have you declared this activity in your AndroidManifest.xml?
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Activity.startActivityForResult(Activity.java:3190)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Activity.startActivity(Activity.java:3297)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at com.jumbybay.activity.CanteenPreference.getUrl(CanteenPreference.java:32)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at com.jumbybay.ferry.FerryMain.onCreate(FerryMain.java:42)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Activity.performCreate(Activity.java:4465)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-07 16:02:11.056: E/AndroidRuntime(1470):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-07 16:02:11.056: E/AndroidRuntime(1470):     ... 11 more
09-07 16:02:11.716: I/dalvikvm(1470): threadid=3: reacting to signal 3
09-07 16:02:11.736: I/dalvikvm(1470): Wrote stack traces to '/data/anr/traces.txt'
09-07 16:07:11.416: I/Process(1470): Sending signal. PID: 1470 SIG: 9

清单文件主要活动

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

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

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

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity android:name=".FerryMain" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <activity android:name=".ferryBlur" >
            </activity>
            <activity android:name=".PassengerList" >
            </activity>
            <activity android:name=".com.jumbybay.activity.LoginActivity" >
            </activity>
            <activity android:name=".TripIn" >
            </activity>
        </application>

    </manifest>

清单文件库

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

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

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

    <uses-feature android:name="android.hardware.nfc" />

    <application>
        <activity
            android:name=".LoginActivity"
            android:label="Jumbybay : Canteen" >
        </activity>
        <activity android:name=".NfcReaderActivity" >
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

尝试删除应用清单中的.

更改

<activity android:name=".com.jumbybay.activity.LoginActivity" >

进入

<activity android:name="com.jumbybay.activity.LoginActivity" >

.表示这是您的Manifest包的一部分,在您的情况下是错误的。

答案 1 :(得分:1)

我预计问题与领先问题有关。您在哪里指定登录活动

.com.jumbybay.activity.LoginActivity

尝试将其更改为com.jumbybay.activity.LoginActivity

您还需要确保在设备上安装相关活动