- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试让 fragment 正常工作,但行为不一致并且很难调试。我使用 xml 文件和类文件创建了两个 fragment 。据我所知,这两个 fragment 的实现方式完全相同。
当我在 MainActivity 中添加“BlankFragment _frag1”时,它会运行。当我添加 BlankFragment2 _frag2 时,当代码退出 MainActivity.onCreate 方法时,它会崩溃。
我在一个更复杂的应用程序中遇到了这个问题,所以我去掉了所有无关的东西,并用我能做的最简单的应用程序来实现它......仍然会发生。
我已在下面附加了 .java 文件和 .xml 文件。我会包含整个项目的 zip,但我没有看到附加文件对话框。如果有人向我发送请求,我可以通过电子邮件发送该项目:darrylctx@gmail.com。
在MainActivity.onCreate方法中,有两行:
_ft.add(R.id.container, _frag1);
_ft.add(R.id.container, _frag2);
如果我注释掉 _ft.add(R.id.container, _frag2);并取消注释//_ft.add(R.id.container, _frag1);然后它运行良好。也就是说,_frag1 运行良好。
如果我注释掉 _ft.add(R.id.container, _frag1);并取消注释 _ft.add(R.id.container, _frag2);它在 onCreate 方法退出时崩溃。也就是说,_frag2 不运行。
据我所知,frag1和frag2的实现没有区别,但我一定错过了一些东西。
// MainActivity.java ------------------------------------------------------------
package com.example.dcornish.TestFrag;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
//import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener {
BlankFragment _frag1 = new BlankFragment();
BlankFragment2 _frag2 = new BlankFragment2();
FragmentManager _fm;
FragmentTransaction _ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// get the viewgroup for this activity
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
//Fragment Help: add fragment to main activity
if (savedInstanceState == null) {
//getSupportFragmentManager().beginTransaction().add(R.id.container, new BlankFragment()).commit();
_fm = getSupportFragmentManager();
_ft = _fm.beginTransaction();
// comment out one or the other of the _ft.add methods below to change fragments
//_ft.add(R.id.container, _frag1);
_ft.add(R.id.container, _frag2);
_ft.commit();
}
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();
}
});
}
public void onFragmentInteraction(Uri uri){
Toast toast = Toast.makeText(this, "Wheeee!",Toast.LENGTH_SHORT);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//BlankFragment.java ----------------------------------------------------
package com.example.dcornish.TestFrag;
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 android.widget.Button;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link BlankFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link BlankFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class BlankFragment extends Fragment implements Button.OnClickListener{
// 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;
private Button mButton; //Add at the top of the fragment
public BlankFragment() {
// 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 BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
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_blank, container, false);
View view = null;
view = inflater.inflate(R.layout.fragment_blank, container, false);
mButton = (Button) view.findViewById(R.id.button);
mButton.setOnClickListener(this);
return view;
}
// 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 Help: Have to figure out what mListener.onFragmentInteraction(null) does
public void onClick(View v){
//Nothing here yet
mListener.onFragmentInteraction(null);
}
}
//BlankFragment2.java --------------------------------------------------
package com.example.dcornish.TestFrag;
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 android.widget.Button;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link BlankFragment2.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link BlankFragment2#newInstance} factory method to
* create an instance of this fragment.
*/
public class BlankFragment2 extends Fragment implements Button.OnClickListener{
// 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;
private Button _button;
public BlankFragment2() {
// 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 BlankFragment2.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment2 newInstance(String param1, String param2) {
BlankFragment2 fragment = new BlankFragment2();
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
View view = null;
view = inflater.inflate(R.layout.fragment_blank_2, container, false);
_button = (Button) view.findViewById(R.id.whee_button);
_button.setOnClickListener(this);
return view;
}
// 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);
}
public void onClick(View v){
//Nothing here yet
mListener.onFragmentInteraction(null);
}
}
//Activity_main.xml -----------------------------------------------------------------
<!-- fragment instructions: android:id = "@+id/container" -->
<!-- give id to the mainActivity layout so it can be referenced as container for fragments -->
<android.support.design.widget.CoordinatorLayout
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:id = "@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.dcornish.TestFrag.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
// content_main.xml
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.dcornish.TestFrag.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
//fragment_blank.xml----------------------------------------------------------------
<FrameLayout 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="com.example.dcornish.TestFrag.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whee"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
//fragment_blank_2.xml -------------------------------------------------
<FrameLayout 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=".BlankFragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TrajMgr"
android:id="@+id/whee_button"
android:layout_gravity="center" />
</FrameLayout>
//menu_main.xml ----------------------------------------------------------------------
<menu 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"
tools:context="com.example.dcornish.TestFrag.MainActivity">
<item
android:id="@+id/mnu_edit_profile"
android:orderInCategory="100"
android:title="Edit Profile"
app:showAsAction="never" />
<item
android:id="@+id/mnu_load_profile"
android:orderInCategory="100"
android:title="Load Profile"
app:showAsAction="never" />
<item
android:id="@+id/mnu_save_profile"
android:orderInCategory="100"
android:title="Save Profile"
app:showAsAction="never" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/mnu_help"
android:orderInCategory="100"
android:title="Help"
app:showAsAction="never" />
<item
android:id="@+id/mnu_dash1"
android:orderInCategory="100"
android:title="------------"
app:showAsAction="never" />
<item
android:id="@+id/mnu_exit"
android:orderInCategory="100"
android:title="Exit"
app:showAsAction="never" />
<item
android:id="@+id/mnu_test"
android:orderInCategory="100"
android:title="Test"
app:showAsAction="never" />
</menu>
最佳答案
我想我可能会明白导致崩溃的原因。当您添加 BlankFragment2
时,您还必须在 MainActivity
OnFragmentInteractionListener
@Override
public void onFragmentInteraction(Uri uri) {
}
它应该看起来像这样
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener, BlankFragment2.OnFragmentInteractionListener {
BlankFragment _frag1 = new BlankFragment();
BlankFragment2 _frag2 = new BlankFragment2();
FragmentManager _fm;
FragmentTransaction _ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// get the viewgroup for this activity
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
//Fragment Help: add fragment to main activity
if (savedInstanceState == null) {
//getSupportFragmentManager().beginTransaction().add(R.id.container, new BlankFragment()).commit();
_fm = getSupportFragmentManager();
_ft = _fm.beginTransaction();
// comment out one or the other of the _ft.add methods below to change fragments
// _ft.add(R.id.container, _frag1);
_ft.add(R.id.container, _frag2);
_ft.commit();
}
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();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
关于java - 一个 fragment 在 .add 之后崩溃,而另一个则没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701047/
我的代码有问题。它总是忽略if(userDigit=1).. 谁能告诉我这里出了什么问题? for(i=0; i=1) { //
我正在尝试从字符串 html_doc 中提取 id=obj1 并尝试将 onclick 函数 附加到它 document.addEventListener("DOMContentLoaded", fu
我正在尝试使用 css 动画来动画化从一个类到另一个类的变化。基本思想是在用户单击按钮时为从一个边缘滑动到另一个边缘的 slider 设置动画。 到目前为止我的代码。 https://jsfiddle
我目前面临使用前后伪元素淡入导航项的问题。 当我悬停导航项时,它必须将其背景颜色从白色更改为蓝色。没什么疯狂的。但它也必须显示两个背景图像,分别通过将::before 伪元素从 0 更改为 1 和::
有没有简单的方法可以在最近的sqlite版本中修改表,使其与预定义的架构匹配? 架构: war_id INTEGER NOT NULL, clanname VARCHAR(64), clanhomep
我该如何将我的搜索结果变成这样的: http://i.stack.imgur.com/NfPGs.png 结果显示特定术语在单元格中的位置。 我目前有这个基本的搜索脚本: $terms =
我正在尝试使用按钮创建输入字段。但我想要的是,当创建输入字段时,我想用相同的按钮隐藏创建的输入字段。我尝试了 slideToggle 函数,但效果不是很好。 $('#addEmail').one('
我想做这样的事情: Reference of image. 我所做的:两个 UIImagesView,一个带有 UIViewContentModeLeft,另一个带有 UIViewContentMod
我在使用应该修复表中列的插入触发器时遇到了问题: id - auto increment int thread_id - int [NULL] 我想要实现的是将 thread_id 设置
我使用 tinter.after() 每 200 毫秒 刷新一次树莓派上模拟时钟的显示。一开始还可以,但逐渐地,每次刷新之间的时间达到大约 2-3 秒。是否有任何解决方案可以将刷新间隔保持在 200m
我有一个按钮,它使用::after 伪来填充背景。目前它从左到右填充,这在宽度从 0 到 100% 时有意义。但是,我希望它翻转它填充的方式。 a.project--link { margin:
我正在尝试添加带有伪元素:after的下划线来注释一些文本。 我的问题是,我想强调下划线。在此示例中,这是短语“实际上确实可以...”和“ ...不起作用”。 .test { margin-top
鉴于此: This is a test It is 有没有我可以应用到 的 CSS?那它会出现在“This is...”之前,并且在 PREVIOUS LINE 之前吗? float:left; d
我正在使用链接左侧的图像。 现在,我使用图像的::before 属性来显示,但它显示在链接的上方。 我需要对齐它。这是一张照片: Link 我使用的代码是: .vocabulary-duration
我有一个页脚有 与 6 body {background:#bbb;} .main-footer a::after { content: " | "; color: white; mar
我有一个父元素和一些子元素,但我不能直接更改它们的 CSS。所以,我试图在父元素的 CSS 中更改我 child 的 CSS。示例: .parent { & .child {
我可以 div:after { content: "hello" } 但我能否为 hello 文本添加标题,以便当我用鼠标悬停它时显示标题? 谢谢 最佳答案 你不需要伪元素: p { ba
CSS 2.1 :after 和 CSS 3 ::after 伪选择器(除了 ::after 旧浏览器不支持)?是否有任何实际理由使用更新的规范? 最佳答案 这是伪类与伪元素的区别。 除了 ::fir
「掏出钥匙开门,然后在黑暗中摸索着墙壁开关的位置,最后将室内的灯点亮。」 这是一个星期之前,我每天晚上下班回家时的固定戏码,也可能是大部分人每天回家时的经历。这种「一对一」的日常琐碎还有许多许
我正在尝试包装 , ,和具有 的元素修复我无法直接编辑的表单上的某些定位。由于某种原因,当我尝试使用以下代码时: $("label").before(""); $("input[type=tex
我是一名优秀的程序员,十分优秀!