문제

Android 개발을 배우고 있습니다. 나는 매우 쉽게해야 할 일에 갇혀 있습니다.

하나의 활동, 2 개의 조각 및 1 인터페이스가있는 앱을 만드는 것.

android:minSdkVersion="11"
android:targetSdkVersion="19
.

이므로 Manager를 사용하여 B 조각 B에 대한 참조를 생성하려고하는 주요 활동 IM. Eclispse가 나에게 어떤 일을 변화 시키라고 말하고 있기 때문에 여기에 붙어 있습니다 (아래 참조) :

내 오도 :`

@Override
    public void respond(int i) {
        // TODO Auto-generated method stub

    FragmentManager manager =getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);

}`
.

이렇게하면 오류 메시지를 얻고 몇 가지 변경 사항을 수행해야합니다. 변경 사항 이후 코드는 이와 같이 보입니다 (그리고 아직 Fragmentb에 도달 할 수 없음) :

    @Override
public void respond(int i) {
    // TODO Auto-generated method stub

    android.app.FragmentManager manager =getFragmentManager();
    android.app.Fragment f2=  manager.findFragmentById(R.id.fragment2);

}
.

추가 세부 사항은 여기에 여기에있는 가져 오기 헤더를 가져옵니다.

  package com.example.modular_ui;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity implements Communicator{....
.

여기에 뭐 없습니다? 전체 지원 .v4 /support.v7은 신인에 대해 조금 혼란 스럽습니다.

편집 : 변경 후 :

    import android.app.Fragment;
import android.app.FragmentManager;
.

및 단편화 팩먼트 확장 Fragmentb에 대한 참조를 여전히 생성 할 수 없습니다 :

@Override
public void respond(int i) {
    // TODO Auto-generated method stub

FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);
.

}

요청한대로 FragmentB 코드를 게시했습니다.

package com.example.modular_ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment {

TextView text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_b, container);
}

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        text = (TextView) getActivity().findViewById(R.id.textView1);
    }
.

주 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.modular_ui.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.modular_ui.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.modular_ui.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragment1"
        android:layout_marginTop="54dp" />

</RelativeLayout>
.

도움이 되었습니까?

해결책

첫째, 귀하의 활동은 단편화를 확장해야합니다.

지원 라이브러리에 대해서.이전 Androids에 기능을 추가하기 위해 도입되었습니다.예를 들어, 조각은 Android 3.0 (SDK NR : 11)에 도입되었다.실제로 (설명서에 따르면) Androids 3.0 <지원 Libary는 제도의 조각의 시스템 구현을 사용합니다.

다른 팁

은 getSupportFragmentManager ()를 사용합니다.지원 라이브러리를 성공적으로 추가 한 후

OP는 지원을 필요로하지 않고 API 11 및 Newer 에 대해 잘 작동 한 솔루션을 갖는 것이 매우 가깝습니다.

그는 조각 을 바꾸려면 가져 오기 문장에서 지원을 바꾸지 않아도됩니다.

두 가지 접근법의 요약. 모든 활동과 파편 처럼 보이는 코드가 있어야합니다.그들을 섞지 마십시오!(모든 파일에서 모든 줄이 필요하지는 않습니다. 필요에 따라 라인을 사용하십시오.)

지원 -V4 접근법

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;      <-- ".support.v4"
import android.support.v4.app.FragmentManager;

... MainActivity extends FragmentActivity ...

... = getSupportFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches
.

API 11+ 접근법

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches
.


위의 한 접근 방식을 사용하여 작성된 프로젝트가 있고 다른 곳에서 코드에 통합되어있는 경우이 행을 찾아 보려면 사용자가 가진 것과 일치하도록 변경하십시오.

간단합니다.

API 레벨 11 아래에 앱을 실행하도록하려면 getSupportFragmentManager()를 사용하십시오.

앱이 11 위의 API 레벨이있는 장치에서 실행되도록하려면 다음을 사용하십시오. getFragmentManger().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top