Tôi đang học phát triển Android. Tôi bị kẹt ở một cái gì đó nên rất dễ dàng.android.support.v4.app.FragmentManager HOẶC android.app.FragmentManager?
Tạo ứng dụng với một Hoạt động, 2 đoạn và 1 giao diện.
Vì vậy, trong hoạt động chính Cố gắng tạo tham chiếu đến Phân đoạn B bằng trình quản lý. Tôi gặp khó khăn ở đây, bởi vì Eclispse là nói cho tôi thay đổi một số thứ (xem dưới đây):
tăng thêm của tôi: `
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager manager =getFragmentManager();
FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);
}`
Nếu tôi làm điều đó này whay tôi nhận được thông báo lỗi và cần phải thực hiện một số thay đổi. Sau khi thay đổi mã trông như thế này (và tôi vẫn không thể đạt 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);
}
Để biết chi tiết thêm, tôi sẽ đặt ở đây cũng là tiêu đề nhập khẩu của Hoạt động:
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{....
gì Tôi có bị thiếu ở đây không? toàn bộ support.v4/support.v7 là một chút khó hiểu cho tân binh.
EDIT: Sau khi thay đổi để:
import android.app.Fragment;
import android.app.FragmentManager;
VÀ mở rộng FragmentActivity tôi vẫn không thể tạo ra một tham chiếu đến FragmentB:
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);
}
Theo yêu cầu tôi đã đăng mã 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 Main
<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>
Thư viện hỗ trợ chủ yếu hỗ trợ ActionBar và một số tính năng chính khác không được hỗ trợ cho đến API 11. Nếu bạn có API tối thiểu là 11, tôi sẽ sử dụng thư viện thông thường để tránh thêm số lượng không cần thiết. Trừ khi có điều gì đó tôi không biết. – zgc7009
Cảm ơn bạn đã phản hồi nhanh chóng. Eclipse tự động triển khai lib hỗ trợ. Vì vậy, bạn đề nghị thay đổi điều này thành: import android.app.Fragment; nhập android.app.FragmentManager; – Foo
Bạn có thể sử dụng một trong hai, nhưng tôi không nghĩ rằng có một nhu cầu nếu bạn ở một API cao hơn 11 hoặc cao hơn như tất cả mọi thứ (mà tôi biết) được hỗ trợ với các thư viện có nguồn gốc tại thời điểm đó. Nếu đó là trường hợp nó chỉ là số lượng lớn trong ứng dụng của bạn và có thể gây ra một nhức đầu cố gắng để nhớ những gì bạn đang sử dụng. – zgc7009