2012-11-26 4 views
0

다음과 같은 경우가 있습니다. 내 응용 프로그램에 2 개의 패키지가 있습니다. com.example.package1; org.otherexample.package2;Android에서 활동을 호출하는 방법

나는이 같은 매니페스트에 선언

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

을 나는 다음과 같은 오류가 나타날 수

<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에서 를 호출 할 :

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

활동을 호출하는 방법? 고마워요.

+0

읽으신가요? [다른 패키지에서 활동 시작] (http://stackoverflow.com/q/2741857/1267661)? – Sam

+0

활동 2의 내용은 무엇입니까? 이 오류는 Activity2에서 발생할 가능성이 큽니다.당신이 통과하지 못했거나 부적절하게 코딩 한 가치를 기대합니다. 줄 번호가있는 전체 스택 추적을 게시해야합니다. – garbagecollector

+0

@DumpHole은 Activity2에 아무것도 없습니다 ... 단지 setcontentview() ... 패키지 호출이 정확하지 않습니다 ... firstpackage/secondpackage.Activity2 ...를 호출하지만 Activity2 ...는 두 번째 패키지에 있습니다. ..not in second.second –

답변

1

유용 여기에 게시 한 내용이 문제의 근원입니다. 방금 예제 프로젝트를 만들었습니다. 여기

는 매니페스트에 내 두 활동 선언이다 : 내 두 번째 활동에서 나는 기본 패키지에서 R을 가져올 수 있다고

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

참고 : 여기에

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

이 relavent MainActivity에서 비트입니다 :

import com.example.packagetesting.R; 

그러나 모든 것을 컴파일하고 올바르게 실행하면 어. 당신이 AnotherActivity이 com.example.anotherpackage

+0

맞음 :) thanks –

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는 누구입니까? 난 단지 하나의 응용 프로그램 .... 응용 프로그램을 가지고 –

+0

내 편집을보고, 조금 실수했다 – jcw

0

난 그냥 코드를 검사에서만 경우에도 다른 패키지 이름을 모두 보여줍니다처럼

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

을 내 로그가 표시됩니다 파일에

또한 유의하십시오. 그것은 내 애플 리케이션에서 일하고있다.

매니페스트 :

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

자바 코드 :

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을 가져와야합니다.

관련 문제