0

은 startActivity를()는 내 버튼을 클릭하면이 응용 프로그램을 충돌, 작동하지 않습니다와 나는 다음과 같은 오류 얻을 :FragmentActivity에서 startActivity (intent) 대신 사용할 대상은 무엇입니까? FragmentActivity에서 어떤 이유

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{ 

Button live; 

private GoogleMap mMap; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 


live=findViewById((R.id.GoToLive)); 

live.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) { 

      Intent startIntent = new Intent(MapsActivity.this, WorldTabBar.class); 
       startActivity(startIntent);     
     } 
    }); 
:

여기
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.name.app/com.example.name.app.WorldTabBar}: java.lang.ClassCastException: com.example.name.app.WorldTabBar cannot be cast to android.app.Activity 

내 코드의를

startActivity()는 FragmentActivity에서 앱을 충돌시킵니다. FragmentActivity는 ".this"를 사용하기로되어 있기 때문에 getActivity()를 사용할 수 없지만 startActivity()에서는 작동하지 않습니다.

난 그냥 조각이 확장 사용하는 대신 FragmentActivity의 시도 rootView 다음 getActivity을 (추가로 사방) onCreateView 방법에에서 onCreate를 변경하고

getActivity().startActivity(startIntent); //<-didn't work, crashes again 

여기, 내 manifest.xml의 :

</activity> 
    <activity 
     android:name=".MainActivity" 
     android:exported="true" 
     android:label="@string/title_activity_main" /> 
    <activity android:name=".WorldTabBar" /> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/myKey" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"></activity> 
</application> 

startActivity() 대신 사용할 수있는 액션이 ​​있습니까? 사실 ...

Intent startIntent = new Intent(MapsActivity.this, WorldTabBar.class); 

...^위의 코드

지금 내 overridePendingTransition 코드가 인식되지 않습니다, 심지어하지만 내가 생각하는 시작 코드없이) startActivity를 (없이 그 자체로 작동합니다. 그래서 내가 FragmentActivity에서 startActivity()처럼 작동하지만 startActivity()가 아닌 코드를 사용할 수 있습니까?

편집 :

WorldTabBar 클래스 : 귀하의 WorldTabBar

public class WorldTabBar extends Fragment { 


    Button world; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.activity_world_tabbar, container, false); 




    world=rootView.findViewById(R.id.GoToWorld); 
      world.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 

        Intent startIntent = new Intent(getContext(), MapsActivity.class); 
        startActivity(startIntent); 

    getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.no_anim); 

        } 
      }); 

    return rootView; 
    } 


    } 
+0

당신이'WorldTabBar'에 대한 코드를 추가하시기 바랍니다 수 : 또 다른 옵션 - - 런타임에 조각을 추가 https://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime

2) 코드 변경은 활동을 만드는가? – Katharina

+0

코드에 WorldTabBar 클래스를 추가했습니다. – iBEK

+0

액티비티 클래스의 프래그먼트를 시작하려면 https://teamtreehouse.com/community/starting-a-fragment-from-an-activity 링크를 확인하십시오. 그것은 당신을 도울 수 있습니다. – Opriday

답변

1

java.lang.ClassCastException: com.example.name.app.WorldTabBar cannot be cast to android.app.Activity

Fragment하지 Activity 클래스 확장합니다.

1) 활동을 추가 한 다음 WorldTabBar 조각을 추가해야합니다.

예를 들어, 의도로 사용에 매니페스트에이 활동을 등록합니다, WorldTabBarActivityActivity를 확장 생성 : 옵션의

하나는 (XML을 사용하여) 활동에 조각을 추가하려면 여기에 설명 : https://developer.android.com/training/basics/fragments/creating.html. 그것은 어떤 클래스를 확장 않습니다 -

public class WorldTabBar extends AppCompatActivity { 
+0

그럼 내가 무엇을 바꾸겠습니까? – iBEK

관련 문제