- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我收到一条消息,上面写着“必须实现 OnFragmentInteractionListener,而且我已经拥有它...
我已经查看了这里提出的每个问题,但没有人帮助我。
谁能帮帮我?
错误:
FATAL EXCEPTION: main
Process: com.example.android.navigationdrawer, PID: 5916
java.lang.RuntimeException: com.example.android.navigationdrawer.MainActivity@3aefae64 must implement OnFragmentInteractionListener
at com.example.android.navigationdrawer.FragmentCamera.onAttach(FragmentCamera.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
MAINACTIVITY(扩展到 OnFragmentInteractionListener)
package com.example.android.navigationdrawer;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
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.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FragmentCamera.OnFragmentInteractionListener {
主要 Activity (FragmentCamera @Override)
@Override
public void onFragmentInteraction(Uri uri) {
}
主要 Activity (ONCREATE)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
主要 Activity ( fragment 交易)
boolean FragmentTransaction = false;
Fragment fragment = null;
//Camera
if (id == R.id.nav_camera) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
ImageView mImageView = (ImageView) findViewById(R.id.Galley1);
//Galley
} else if (id == R.id.nav_gallery) {
fragment = new FragmentCamera();
FragmentTransaction = true;
}
...
if(FragmentTransaction) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.drawer_layout, fragment)
.commit();
item.setChecked(true);
getSupportActionBar().setTitle(item.getTitle());
}
fragment 相机
package com.example.android.navigationdrawer;
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;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FragmentCamera.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FragmentCamera#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentCamera 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 FragmentCamera() {
// 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 FragmentCamera.
*/
// TODO: Rename and change types and number of parameters
public static FragmentCamera newInstance(String param1, String param2) {
FragmentCamera fragment = new FragmentCamera();
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_camera, 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);
}
}
如果您需要更多代码,请发表评论,我会添加!
最佳答案
从你的 Fragment 中删除 onAttach
方法,然后就可以了。
关于Android Studio 获取 "Must implement OnFragmentInteractionListener",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276576/
我正在尝试查找first_row = [1, 2, 3, 4, ...] 目前,我有 list = [[1, a], [2, b], [3, c], [4, d], ...] 并尝试运行:list[:
我正在尝试查找first_row = [1, 2, 3, 4, ...] 目前,我有 list = [[1, a], [2, b], [3, c], [4, d], ...] 并尝试运行:list[:
我正在尝试构建一个应用程序以提交到 AppStore.. 每次我尝试构建它时,我都会在验证期间收到一条警告: The dwarfdump binary must exist and must be e
我正在关注这个“ Hello World ”教程: https://steemit.com/eos/@skenan/eos-development-for-beginners-webassembly
所以有点背景。我一直在尝试在 CentOS 6 机器上设置 Hive。我按照这个 Youtube 视频的说明操作:http://www.youtube.com/watch?v=L2lSrHsRpOI
我是 graphQL 新手,我正在尝试在后端的resolvers.js 文件下更新用户配置文件密码,但我已经坚持了一段时间,并且不断收到相同的错误消息。 resolvers.js updatePass
我有一个程序正在使用用户指定的测量值来计算矩形的面积。出于特定原因,我正在使用一个类来执行此操作,但是我的编译器会生成两个错误... expression must have class type l
在处理数据结构类的图类时,我遇到了边缘对象问题: myGraph.nodes[a]->edges.back.n1 = myGraph.nodes[a]; myGraph
我正在将此方法添加到公共(public)静态类 public static LogMessage(Exception ex) { Trace.WriteLine(ex.
我经常用 Cache-Control: no-cache 或 Cache-Control: max-age=0 规范说must-revalidate是为了max-stale...(服务器问题max-s
每当从列出的类中运行下面的行时,我在版本 2017.2.0f3 和 2017.2.1f1 中遇到此错误,但它在 5.5.0.f3 中完美运行 WWW request = new WWW(m_host,
我已经在我的 Ubuntu EC2 实例上安装了 Hadoop,并按照本教程完成了安装 hive 的所有步骤:http://www.tutorialspoint.com/hive/hive_insta
您好,在此先感谢您的帮助! 当我尝试执行从 GitHub 提取的时间序列分解时,出现ValueError: You must specify a period or x must be a panda
为了寻求有关问题的建议和帮助,我在运行 Xubuntu Linux 16.04 LTS 的笔记本电脑上新安装了 Oracle XE。我关注了this发布我的安装。 在 Oracle XE 安装位置 /
当通过 AD 帐户登录时,我在尝试在 C# MVC Web 应用程序中加载 p12 证书文件时遇到问题。 我们在加载证书时遇到的错误是:必须信任计算机才能进行委托(delegate),并且当前用户帐户
我想使用 IOptions 通过 POCO 获取配置,但它会抛出错误消息“模型绑定(bind)的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数” DatabaseSettings.cs p
正确执行加法模型有一些问题。 我有那个数据框: 当我运行此代码时: import statsmodels as sm import statsmodels.api as sm decompositio
感谢有关 tutorialspoint 和 stackoverflow 的有用信息,我几乎完成了在 Oracle VirtualBox 上的 Ubuntu 上安装 Hive 3.1.1 和 Hadoo
这是我用于测试目的的简单代码。 boolean isMoving(){ if (a == b) { return true; } else if (a != b) {
如果我的模型中有这样的属性: [BindProperty] public IPagedList Products { get; set; } 然后当我尝试发布时,我收到此错误: An
我是一名优秀的程序员,十分优秀!