2012-11-26 29 views
0

我有以下情形。 我在我的應用程序中有2個軟件包。 com.example.package1; org.otherexample.package2;如何在Android中調用活動

我申報清單是這樣的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.package1" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <activity android:name=".ActivityfromPackage1"/> 
    <activity android:name="org.otherexample.package2.ActivityFromPackage2"/> 

</manifest> 

這是清單,現在我想從ActivityFromPackage1 ActivityFromPackage2通話 我做這樣的:

import org.otherexample.package2.ActivityFromPackage2 
.......... 
Intent intent = new Intent(this,ActivityFromPackage2.class); 
startActivity(intent); 

我收到以下錯誤:

Unable to start Activity com.example.package1/org.otherexample.package2.ActivityFromPackage2: 
JavaLang nullpointer exception 

如何調用活動? 非常感謝。

+0

你看過:[從不同包裝發佈活動](http://stackoverflow.com/q/2741857/1267661)? – Sam

+0

Activity2中有什麼?該錯誤很可能來自Activity2。它可能期望你沒有通過的一些值,或者不正確的編碼。您應該使用行號發佈完整的堆棧跟蹤。 – garbagecollector

+0

@DumpHole沒有什麼是在Activity2中...只是一個setcontentview()...包的調用是不正確的...正在調用firstpackage/secondpackage.Activity2 ...但Activity2 ...只是在第二個包中。 ..不在第一。第二 –

回答

1

我懷疑的東西外你在這裏發佈的是你問題的根源。我剛做了一個示例項目來測試它。

這裏是我的兩個活動的聲明在清單:

 <activity 
      android:name="com.example.packagetesting.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.anotherpackage.AnotherActivity" 
      android:label="@string/title_activity_another" > 
     </activity> 

這裏是MainActivity的relavent位:

import com.example.anotherpackage.AnotherActivity; 
... 
Intent i = new Intent(this, AnotherActivity.class); 
startActivity(i); 

注意,在我的第二個活動,我不得不從主包導入[R :

import com.example.packagetesting.R; 

但之後,所有的東西都編譯並運行正確LY。

另外,請注意我的日誌文件它顯示:

Starting: Intent { cmp=com.example.packagetesting/com.example.anotherpackage.AnotherActivity } 

哪像你同時顯示不同的包名的,即使AnotherActivity只是com.example.anotherpackage

+0

正確:)謝謝 –

0

我認爲你可能需要修改您的清單一點點,儘量使它看起來更像這個

<activity android:name=".ActivityFromP2"> 
    <intent-filter> 
    <action android:name="package2.intent.action.Launch" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

這個問題是非常有用

launch activities from different package

+0

誰是應用程序B?我只有一個應用程序....應用程序A –

+0

看到我的編輯,提出了一個小錯誤 – jcw

0

我只是檢查你的代碼。它在我的應用程序中工作。

清單:

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

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

    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="org.pac.abcs.TestActivity" > 
     </activity> 
    </application> 

</manifest> 

Java代碼:

btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       startActivity(new Intent(MainActivity.this, TestActivity.class)); 
      } 
     }); 

所以它不是包聲明的問題。只有一個疑問,你有沒有在你的第二個活動(ActivityFromPackage2)或com.example.package1.R中導入android.R?您將需要導入com.example.package1.R。

相關問題