Hello I am wondering how to clear a render error I am having with a fragment which is saying unknown fragment. I can seem to find what I did wrong to fix it. I have a recycleview in the fragment that is just a test to make sure that is working as well right now.
Here is the Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="jlfletcher.cpsc4367.ualr.edu.hw2.MainActivity">
<fragment
android:id="#+id/fragmentone"
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is the Java of the fragment
package layout;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import jlfletcher.cpsc4367.ualr.edu.hw2.CPSClist;
import jlfletcher.cpsc4367.ualr.edu.hw2.R;
public class FragmentOne extends Fragment
{
public void OnCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View View = inflater.inflate(R.layout.fragment_fragment_one, container, false);
RecyclerView recyclerView =
(RecyclerView)View.findViewById(R.id.Class_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
CPSClist textAdapter= new CPSClist();
recyclerView.setAdapter(textAdapter);
List<String> stringList = new ArrayList<>();
stringList.add("This");
stringList.add("is");
stringList.add("a");
stringList.add("of");
stringList.add("the");
stringList.add("recycle");
stringList.add("view");
stringList.add("system");
stringList.add("this");
stringList.add("is");
stringList.add("only");
stringList.add("a");
stringList.add("test.");
List<String> list = new ArrayList<>();
list.addAll(stringList);
list.addAll(stringList);
list.addAll(stringList);
list.addAll(stringList);
list.list(stringList);
list.addAll(stringList);
textAdapter.setItems(list);
}
}
Thanks again for the help on this.
Related
I have a TabLayout on a activity and when user clicks on any tab then a fragment is loaded. One of those fragments contains a NavigationView running inside a DrawerLayout. Now the problem is I have to implement MVVM for the whole app and so for the fragments too, I have done this for Activities but I am having trouble while implementing this for Fragments and how to deal with ItemSelected with MVVM and what to bind and how? I am unable to find a proper working sample of the same. Here's the code:
MoreFragment.java:
package com.abc.Fragments;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
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.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.abc.MainActivity;
import com.abc.MoreFragmentViewModel;
import com.abc.R;
public class MoreFragment extends Fragment {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(MoreFragmentViewModel.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.fragment_more, container, false);
return fragView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDrawerLayout = getView().findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(Gravity.END);
}
}
fragment_more.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/grey"
app:navigationItemSelectedListener="#{()-> viewModel.onMyItemSelected()}"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
MoreFragmentViewModel.java:
package com.abc;
import android.arch.lifecycle.ViewModel;
import android.databinding.BaseObservable;
public class MoreFragmentViewModel extends ViewModel {
public void onMyItemSelected(){
System.out.print("Item Clicked");
}
}
Please do any possible help.
Use:
DataBindingUtil.inflate(inflater, layoutId(), container, false)
To inflate your layout.
And don't forget to add:
<data>
<variable
name="model"
type="MoreFragmentViewModel"/>
</data>
To your fragment layout.
I try add DFP Banner Ads to my app , All steps have been applied in https://developers.google.com/ad-manager/mobile-ads-sdk/android/banner .
but if I open my app it crash
crash:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.doubleclick.PublisherAdView.loadAd(com.google.android.gms.ads.doubleclick.PublisherAdRequest)' on a null object reference
at com.englishprofor.all.fragment.FragmentCategory.onCreateView(FragmentCategory.java:61)
FragmentCategory.class
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.englishprofor.all.AppController;
import com.englishprofor.all.Constant;
import com.englishprofor.all.R;
import com.englishprofor.all.activity.MainActivity;
import com.englishprofor.all.activity.SettingActivity;
import com.englishprofor.all.helper.CircleImageView;
import com.englishprofor.all.helper.SettingsPreferences;
import com.englishprofor.all.model.Category;
import com.englishprofor.all.model.SubCategory;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
import com.google.android.gms.ads.doubleclick.*;
import java.util.ArrayList;
public class FragmentCategory extends Fragment {
private static final String TAG = "FragmentCategory";
Context con ;
private RecyclerView recyclerView;
private View view;
ArrayList<Category> mListItem;
//AdView mAdView;
private PublisherAdView mPublisherAdView;
TextView empty_msg, tvTitle;
CoordinatorLayout layout;
ImageView back, setting;
public static ArrayList<Category> categoryList;
public static ArrayList<SubCategory> subCatList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_category, container, false);
mPublisherAdView = view.findViewById(R.id.publisherAdView);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
mPublisherAdView.loadAd(adRequest);
fragment_category.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="#layout/actionbar_layout"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/category_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout"
android:layout_above="#+id/banner_AdView"/>
<TextView
android:id="#+id/txtblanklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#color/black"
android:textSize="15dp"
android:textStyle="bold"
android:visibility="gone" />
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/banner_AdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="/6499/example/banner">
</com.google.android.gms.ads.doubleclick.PublisherAdView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
please help me , I want enable DFP in my app
thanks for all
You got a wrong id of view in below LOC, that why mPublisherAdView be NULL
mPublisherAdView = view.findViewById(R.id.publisherAdView);
should be
mPublisherAdView = view.findViewById(R.id.banner_AdView);
I have already looked at the other answers but none of them help me.
I made sure to use import android.support.v4.app.Fragment; so that's not the problem.
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
Error :
Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
Why am I getting this?
In activity_main.xml I have a FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:id="#+id/fragmentContainer">
</FrameLayout>
myFragment.java
package com.example.nirvan.fragmentsexample2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
return inflater.inflate(R.layout.fragment,container,false);
}
public myFragment(){}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="145dp"
android:text="fragment One"/>
</RelativeLayout>
Sloppy mistake . You should call v4.app.Fragment instead of app.Fragment .
Open myFragment.java
Don't
import android.app.Fragment;
Do
import android.support.v4.app.Fragment;
Change this
import android.app.Fragment;
to
import android.support.v4.app.Fragment;
in myFragment class
Also i would suggest to have proper naming convention for your fragment.
No need for android:orientation="vertical" in relative layout.
I have a RecyclerView with a TextView in it's place as well for when no results are found.
This TextView when calling root.findViewById always returns null, I have tried re-cleaning the project, building the project over again. Nothing has helped.
Here are my imports:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.mypackage.commoncode.R;
Here is the code for the onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_mail, container, false);
//
mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
mProgressBar.setVisibility(View.VISIBLE);
final Context context = root.getContext();
mEmptyView = (TextView) root.findViewById(R.id.empty_view);
mEmptyView.setVisibility(View.INVISIBLE);
// set list
float ht = BitmapUtils.convertDpToPixel(8, context);
mLinearLayoutManager = new LinearLayoutManager(context);
mListView = (RecyclerView) root.findViewById(android.R.id.list);
mListView.setLayoutManager(mLinearLayoutManager);
mListView.setVisibility(View.INVISIBLE);
mListView.addItemDecoration(new VerticalSpaceItemDecoration(Float.valueOf(ht).intValue()));
mListAdapter = new InboxListAdapter(context);
mListAdapter.setOnInboxClickListener(this);
mListView.setAdapter(mListAdapter);
if (mBackground == null) {
mBackground = new HandlerThread("background", android.os.Process.THREAD_PRIORITY_BACKGROUND);
mBackground.start();
mBackgroundCallback = new BackgroundCallback(this.getActivity().getApplicationContext(), mCurrentKid);
mBackgroundHandler = new Handler(mBackground.getLooper(), mBackgroundCallback);
}
final Handler displayHandler = new Handler(new HandlerCallback(this));
mBackgroundCallback.setDisplayHandler(displayHandler);
//
retrieveInboxList(context);
return root;
}
Here is the XML for the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal" />
<android.support.v7.widget.RecyclerView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="invisible"
android:text="#string/No_Results_found" />
</FrameLayout>
Any ideas as to why the empty_view TextView is always null?
Logcat error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at this line:
mEmptyView.setVisibility(View.INVISIBLE);
The view xml referenced does not have the view with the id your are trying to retrieve. Make sure the xml posted is the correct one or that you have another xml for this resolution that maybe is not containing the view.
I have to create an application in which I have to work with Fragment.
In the MainActivity, there is a webView. From the SecondActivity I
have starting using Fragment.
Here is the code of SecondActivity:
package com.dev.testapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class Second extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public void choosefragment(View view) {
Fragment fg;
if(view == findViewById(R.id.secondbtn2)) {
fg = new SecondFragment();
}
else
{
fg = new FirstFragment();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frag1, fg);
fragmentTransaction.commit();
}
}
Hhere is its second.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<Button
android:id="#+id/secondbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first fragment"
android:onClick="chooseFragment" />
<Button
android:id="#+id/secondbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second fragment"
android:onClick="chooseFragment"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.dev.testapp.fragment1"
android:id="#+id/frag1"
/>
</LinearLayout>
After that I created two more classes for Fragments as it is required
code of FirstFragment:
package com.dev.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1,container, false);
}
}
And here is the SecondFragment:
package com.dev.testapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2,container, false);
}
}
When I am done with all this, my SecondActivity is showing an error that I don't know how to handle:
As noted in previous answers, your problem is incorrect Fragment library for FirstFragment.
Change line 5 of FirstFragment.java:
import android.support.v4.app.Fragment;
to:
import android.app.Fragment;
Your second fragment is of the type android.app.Fragment while your first fragment is android.support.v4.app.Fragment.
your activity use this import android.app.Fragment;
your fragment is import android.support.v4.app.Fragment;
So you either have both extending the Fragment in the app package or in the support library