홈>
프래그먼트에 대해 공부하고 있으며 기본 예제를 수행하고 있지만 작동하지 않습니다.
2 가지 활동이있는 기본 예제가 있습니다 : -이미지와 텍스트보기가있는 하나의 활동이며 이미지를 클릭하면 일부 항목이있는 목록보기가있는 다른 활동으로 이동합니다. 이 목록보기를 클릭하면 기본 활동으로 돌아가고 텍스트보기에서 클릭 한 목록 항목과 관련된 텍스트를 표시합니다.
그러나 이제이 기본 예제를 통과하려고했지만 활동이 하나만있는 조각을 사용했지만 작동하지 않는 것은 이미지를 클릭 할 때 항상 "앱이 중지되었습니다"라고 나타납니다.
내가하는 일, 그리고 최선의 해결책인지 모르겠지만, 목록보기와 함께 조각이 나타나도록 클릭해야하는 이미지만으로 주요 활동을하는 것입니다. 하나는 목록보기를 표시하고 다른 하나는 클릭 한 항목의 텍스트와 함께 텍스트보기를 표시하는 두 개의 조각으로 구성됩니다.
지금은 첫 번째 부분을 수행하는 것입니다. 즉, 이미지를 클릭하고 목록보기로 조각을 표시하고 조각의 항목을 클릭하면 토스트 메시지가 표시됩니다. 그러나 작동하지 않지만 항상 "앱이"중지되었습니다 "로 나타납니다.
내가 가진 것은 주요 활동에서 :
public class MainActivity extends AppCompatActivity {
private ImageView img;
private TextView tv;
Intent intent;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
img = (ImageView) findViewById(R.id.imageView);
tv = (TextView) findViewById(R.id.textView);
}
public void AddFragmentList(View view) {
Fragment fragment = new Fragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.listviewInFragment, fragment, "frag");
transaction.commit();
}
}
주요 활동 XML :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/image"
android:onClick="AddFragmentList"
tools:layout_editor_absoluteY="71dp"
tools:layout_editor_absoluteX="0dp" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="11dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="110dp">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
조각 java :
public class fragment extends Fragment {
private ListView listItems;
private String[] items = {
"item1",
"item2",
"item3",
"item4"
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listItems = (ListView) listItems.findViewById(R.id.listviewInFragment);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, items);
listItems.setAdapter(adapter);
listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int positionCode = i;
String clickedValue = (String) listItems.getItemAtPosition(positionCode);
Listener listener = (Listener) getActivity();
listener.addText(clickedValue);
Toast.makeText(getActivity().getApplicationContext(), clickedValue, Toast.LENGTH_SHORT).show();
}
});
}
}
그리고 xml 조각에서 :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0ff">
<ListView
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="16dp"
android:id="@+id/listviewInFragment"/>
</android.support.constraint.ConstraintLayout>
- 답변 # 1
관련 자료
- android - 활동 xml의 조각에있는 findViewById 속성이 작동하지 않음
- android - 조각 활동에서 버튼이 작동하지 않습니다
- android - 활동에서 조각으로 데이터를 전달하려고 할 때 앱이 다운됩니다
- android - 조각에서 활동으로 제어를 되 돌리는 방법은 무엇입니까?
- java - 프래그먼트에서 활동을 시작하는 데 사용되는 액세스 의도
- android - 활동에서 조각의 텍스트를 텍스트보기로 설정
- java - 프래그먼트에서 작동하지 않는 방송 관리자
- android - 조각에서 Google지도를 사용하는 동안 작동하지 않지만 활동에서 동일한 맵이 작동하는 이유는 무엇입니까?
- java - Viewpager 내부의 활동에서 단편으로 인 텐트 엑스트라를 전달할 수 없습니다
- android - Viewpager 및 Fragment Placeholder를 사용하여 탭 활동에서 OnClickListener를 어떻게 사용합니까?
- android - BottomSheet Fragment에서 항목을 선택할 때 활동 새로 고침
- 안드로이드에서 조각에서 활동으로 데이터를 보내는 방법
- android - 활동에서 조각으로 뒤로 단추를 추가하는 방법
- android : Hilt로 활동 또는 조각을 바인딩 /제공하는 방법?
- android - 스크롤 조각은 활동보기 아래의 활동 원인 아래에 나타납니다
- java - SharedPrefarence에서 조각 활동으로 가치를 얻는 방법?
- android - 조각을 포함하여 두 번째 활동을 닫는 방법은 무엇입니까?
- java - 조각에 이러한 활동 코드를 구현하는 방법은 무엇입니까?
- android - 활동 또는 조각에서 실 데이터베이스를 초기화 하시겠습니까?
- android - 클래스가 YouTubeBaseActivity를 확장 할 때 조각에서 활동으로 이동하는 방법
관련 질문
- android : 조각 내부의 ListView에서 링크 URL을 여는 방법은 무엇입니까?
- android : Dialogfragment에서 결과를받습니다
- android : 조각과 그 컨테이너 활동간에 데이터를 전달합니다
- java.lang.nullPointerException : ImageView.SetImagedAlable (android.graphics.drawable.drawable)
- java : 조각으로 onCreate ()에서 변수의 데이터를 보내십시오.
- java : NullPointerException은 조각에서 활동 방법을 호출 한 후에 있습니다
- java : 조각간에 별도의 데이터로 특정 데이터를 선택할 수있는 방법이 있습니까?
- java : Fragment의 OnRefresh SwipereFreshlayout 내 주요 활동 클래스 메소드 내 조각을 대체하는 방법
- android : 조각에서 활동까지 값을 전달하는 방법
- android : GetContext ()가 Fragment의 이유가 때때로 NULL을 반환합니까?
FrameLayout
의 아이디가 있습니다그런 다음 변경
to
마지막으로 변경
to
외에 당신은 활동에 대한 callbaclk로서 인터페이스를 가질 수 있습니다. 리스너
에 도움이됩니다.listener = (Listener) getActivity();listener.addText(clickedValue);
로 무엇을하고 있는지 잘 모르겠습니다. https://developer.android.com/training/basics/fragments/communicating.html