我正在向片段中的操作栏添加制表符.但是当我尝试启动该片段时,我的应用程序在抛出空指针异常后崩溃
这是错误
Home.java文件是这样的:
public class Home extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabPagerAdapter tabPagerAdapter;
private ActionBar actionBar;
private FragmentActivity fragmentActivity;
private String[] tabs ;
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
fragmentActivity = new FragmentActivity();
tabs = getResources().getStringArray(R.array.hometabs);
viewPager = (ViewPager)rootView.findViewById(R.id.pager);
actionBar = fragmentActivity.getActionBar();
tabPagerAdapter = new TabPagerAdapter(fragmentActivity.getSupportFragmentManager());
viewPager.setAdapter(tabPagerAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(String tab_name: tabs){ …Run Code Online (Sandbox Code Playgroud) 我正在尝试在片段的onCreateView方法中执行下一个代码:
if (displayedPalabra!=null){
setCardAttributes(displayedPalabra);
NavigationView navigationView = (NavigationView) layout.findViewById(R.id.nav_view);
navigationView.getMenu().getItem(0).setChecked(true);
}
Run Code Online (Sandbox Code Playgroud)
但是在执行时给出了下一个错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.Menu android.support.design.widget.NavigationView.getMenu()'
我的问题是:Fragment的布局是否无法从MainActivity访问NavigationView并因此检索空值?我是否必须以其他方式访问它?代码应该是不言自明的,我只是试图在我的应用程序的NavigationView菜单中设置此Fragment的项目...通常我从MainActivity执行以放置默认值但这次从另一个片段调用此Fragment ...
请帮助我获取权限“ WRITE_EXTERNAL_STORAGE”和“ WRITE_SETTINGS”的请求代码。还请让我知道请求代码的来源。
performance android android-layout android-fragments android-studio
所以我有一个片段内的recyclelerview,当内容加载到recyclerview时,它不占用屏幕的整个宽度.奇怪的是,我将这个代码用于另一个项目实际上是相同的并且没有问题.
片段布局是这样的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/windowBackground"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.dishesteam.dishes.activities.HomeActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="4dp"
android:paddingBottom="4dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/latest_recycler">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_dish_fab"
app:srcCompat="@drawable/ic_add_black_24dp"
app:fabSize="normal"
app:backgroundTint="@color/colorAccent"
android:clickable="true"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</android.support.design.widget.CoordinatorLayout>
<ImageView
android:id="@+id/trending_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_trending_up_black_24dp"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="Nothing here at the moment"
android:textSize="16sp"
android:id="@+id/empty_text"
android:layout_below="@+id/trending_img"
android:layout_centerHorizontal="true"
android:textColor="@color/ColorDarkGrey" />
Run Code Online (Sandbox Code Playgroud)
recyceler的各个项目的布局是
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/textColorPrimary"
app:cardCornerRadius="4dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">
<LinearLayout …Run Code Online (Sandbox Code Playgroud) xml android android-layout android-fragments android-recyclerview
我正在实现EventBus lib传递并从任何类或片段获取数据的方法,还订阅了一种方法来获取即时更改的数据...但是我收到以下错误消息:
org.greenrobot.eventbus.EventBusException:订户类java.lang.Boolean及其超类没有带@Subscribe批注的公共方法
我已经订阅了github 显示的方法
程式码片段
public class ItemFragment extends Fragment {
View view;
String data;
RecyclerView recyclerView;
CategoryAdapter itemAdapter;
List<Category.Items> list;
private String TAG = getClass().getName();
public static ItemFragment newInstance(String detail) {
Bundle args = new Bundle();
ItemFragment fragment = new ItemFragment();
args.putString("data", detail);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.cat_recy, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { …Run Code Online (Sandbox Code Playgroud) 我不断收到有关我的静态片段实例的内存泄漏的警告。我声明如下:
私有静态myFragment thisFragment;
并在这里调用:
public static myFragment newInstance() {
if (thisFragment == null) {
thisFragment = new myFragment();
}
return thisFragment;
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决?有任何想法吗?
谢谢!
android memory-leaks android-context android-fragments android-memory
在我得到的最后一种方法中
Type mismatch: inferred type is android.support.v4.app.Fragment
but android.app.Fragment! was expected
Run Code Online (Sandbox Code Playgroud)
我不确定如何在Kotlin解决这个问题.
var fragment: Fragment = null
var fragmentClass: Class<*>? = null
fragmentClass = CardBackFragment::class.java
try {
fragment = fragmentClass!!.newInstance() as Fragment
} catch (e: Exception) {
e.printStackTrace()
}
fragmentManager
.beginTransaction()
.replace(R.id.flContent, fragment)
Run Code Online (Sandbox Code Playgroud) 我需要将数据从Fragment发送到另一个活动
我在HomeActivity下的LoadsFragment中使用此代码
Intent intent = new Intent(activity, LoadActivity.class);
intent.putExtra("loadsPosition",position);
activity.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在另一个活动(LoadActivity)中接收数据
Intent intent=getIntent();
String loadsPosition = intent.getStringExtra("loadsPosition");
Run Code Online (Sandbox Code Playgroud)
但意图没有额外的东西
请参阅下面的屏幕截图
我有fragment一个nestedScrollView一些静态信息和一些LinearLayouts有知名度GONE。
这些LinearLayout在fragment打开后得到填充,然后在VISIBLE上设置这些布局的可见性。我只想在加载此信息时滚动到该片段的底部。
我试图与物业descendantFocusability并使用fullscroll function这个nestedScrollView,但我没有得到任何结果。
一切工作正常,此代码我从没有任何问题。但是以某种方式,当我尝试接收不在第一个位置的Tab时,现在得到一个空对象:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.support.design.widget.TabLayout $ Tab android.support.design.widget.TabLayout $ Tab.setCustomView(android.view.View)'
在onCreateView中调用tabLayoutSetup。
private void tabLayoutSetup(){
// Get the ViewPager and set it's PagerAdapter so that it can display items
Bugger.t(context,"tablayout");
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
createTabs();
}
//this method is creating all tabs for account's tablayout
private void createTabs(){
TabLayout.Tab ShareTab = tabLayout.getTabAt(0);
TabLayout.Tab MarkTab = tabLayout.getTabAt(1);
//***********************************************************************************************************
//inflating template for first tab, Activities.
View TabView = LayoutInflater.from(context).inflate(R.layout.account_tablayout_customtab, null); //template
TextView titel = (TextView) TabView.findViewById(R.id.tab_titel);
//populating first template for 'Activities'
titel.setText(getResources().getString(R.string.SharesTab)); …Run Code Online (Sandbox Code Playgroud) android nullpointerexception android-layout android-fragments android-tablayout