gpt4 book ai didi

java - Android 运行时异常 :Activity must implement OnFragmentInteractionListener

转载 作者:行者123 更新时间:2023-11-29 04:39:49 25 4
gpt4 key购买 nike

我正在尝试使用 fragments 实现一个抽屉布局

content_main.xml(应显示 fragment 的位置):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.daveslab.wideview.MainActivity"
tools:showIn="@layout/app_bar_main"
android:id="@+id/FragmentPlaceLayout">
</RelativeLayout>

MainActivity.java:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.FragmentManager;
import layout.FragmentMain;
...other stuff...
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {
FragmentMain fragment_main = FragmentMain.newInstance("one","two");
FragmentManager manager= getSupportFragmentManager();
manager.beginTransaction().replace(R.id.FragmentPlaceLayout,fragment_main).commit();
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.FragmentMain">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</RelativeLayout>

FragmentMain.java:

package layout;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daveslab.wideview.R;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FragmentMain.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FragmentMain#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentMain extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public FragmentMain() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentMain.
*/
// TODO: Rename and change types and number of parameters
public static FragmentMain newInstance(String param1, String param2) {
FragmentMain fragment = new FragmentMain();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

当我点击抽屉按钮时出现错误控制台日志:

09/25 11:16:55: Launching app
....
Success


$ adb shell am start -n ....

[ 09-25 09:16:58.565 2534: 2534 D/ ]
HostConnection::get() New Host Connection established 0xad13bdf0, tid 2534
I/OpenGLRenderer: Initialized EGL, version 1.4
W/PathParser: Points are too far apart 4.000000596046461
D/AndroidRuntime: Shutting down VM


--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.daveslab.wideview, PID: 2534
java.lang.RuntimeException:com.daveslab.wideview.MainActivity@aa561a9 must implement OnFragmentInteractionListener
at layout.FragmentMain.onAttach(FragmentMain.java:84)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 2534 SIG: 9
Application terminated.

一些想法?

最佳答案

您的 Activity 没有 OnFragmentInteractionListener,因此如果未找到它,那么您的代码将在 else case 中抛出此异常。

if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() // Here is your exception
+ " must implement OnFragmentInteractionListener");
}

在您的 MainActivity 中执行此操作。

public class MainActivity extends AppCompactActivity implements 
NavigationView.OnNavigationItemSelectedListener
,FragmentMain.OnFragmentInteractionListener{

@Override
public void onFragmentInteraction(Uri uri){

}
}

关于java - Android 运行时异常 :Activity must implement OnFragmentInteractionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39685267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com