Can somebody explain what is the problem here? The error is bellow:
Code in MainAcitivity:
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = (SvePonudeFragment) fm.findFragmentById(R.id.ponudice);
fragment.reloadData();
Fragment Class:
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ArrayList<Ponuda> listaPonuda = null;
initializeAdapter(listaPonuda);
return rootView;
}
private void initializeAdapter(List<Ponuda> preuzetePonude){
RVAdapter adapter = new RVAdapter(preuzetePonude);
rv.setAdapter(adapter);
}
public void reloadData(){
System.out.println("I ENTEEERED");
}
}
XML of fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/ponudice">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/rv"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
What is wrong with this? Can somebody explain me?
this is the error:
W/System.err: java.lang.NullPointerException
W/System.err: at com.example.filip.dajsve.Activities.MainActivity.onDataLoaded(MainActivity.java:190)
W/System.err: at com.example.filip.dajsve.Loaders.DatabaseDataLoader.loadData(DatabaseDataLoader.java:22)
I found the problem. The problem is that you don't link your fragment with mainActivity. first you should add your fragment in xml of mainActivty and give the fragment an id then access it in the main activity with the id of fragment not using this id android:id="#+id/ponudice"> because this id related with relative layout not the fragment. check the xml code of main activity you should find fragment tags like code below
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="theNameOfYourPackage.theNameOfFragment"
android:id="#+id/ponudice_in_main_activity"/>
Do it like this
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = new SvePonudeFragment();
fm.beginTransaction().replace(R.id.framecontainer,fragment).commit();
and your activity_main.xml file will be
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/framecontainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="16dp"
>
</FrameLayout>
Related
I've been looking for the cause of this exception in answers for similar issues, and couldn't find what's wrong with my code. I changed the method that instantiates a fragment a few times and got the same exception. Maybe it has something to do with the splash screen. Couldn't find information about that.
Here's the beginning of the MainActivity in which the method for instantiating a fragment is called:
public class MainActivity extends AppCompatActivity {
Context context;
FragmentTransaction ft;
Fragment mlf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
File file = new File(path);
if (file.exists()) {
Log.d(TAG, path + " exists");
new DBConnection(MainActivity.this);
if(savedInstanceState == null) {
toMovieListFragment();
}
//...
//...
Here's the method:
private void toMovieListFragment() {
mlf = new MovieListFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_ontainer, mlf);
ft.addToBackStack(null); // add to back stack
ft.commit();
}
}
The MainActivity's xml:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragments_container">
</LinearLayout>
The fragment class:
public class MovieListFragment extends Fragment {
Context context;
TextView listTtl;
RecyclerView rvMovies;
Adapter moviesAdapter;
List<Movie> moviesList;
Fragment mdf;
FragmentTransaction ft;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_list_movies, container, false);
context = getActivity();
listTtl = rootView.findViewById(R.id.moviesListTtlId);
rvMovies = rootView.findViewById(R.id.moviesRVId);
moviesList = new ArrayList<>();
moviesAdapter = new Adapter(context, moviesList);
rvMovies.setAdapter(moviesAdapter);
// setting a layout manager
rvMovies.setLayoutManager(new LinearLayoutManager(context)); // a regular one
Log.i(TAG,"In movieListFragment");
return rootView;
}
}
The fragment class's xml file:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/moviesListTtlId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dot_height"
android:text="#string/moviesListTitle"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/ttlSize" />
<android.support.v7.widget.RecyclerView
android:id="#+id/moviesRVId"
android:layout_margin="#dimen/dimen_10"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<include layout="#layout/change_name" />
Thanks.
For some reason your main activity layout is called R.layout.activity_splash I believe your intention was for it to be whatever main activity layout is called (the one that has R.id.fragments_container inside).
I am trying to make a Login screen where the Register, Login and Forgot Password screens are all fragments, placed like this:
Forgot Password | Login | Register
I found a simple video on YouTube showing me how to do this. The only issue I have is that when I start the Activity, it opens on the Forgot Password fragment since it is the first one, but I am wanting it to start in the middle, the Login fragment. Here is the code for the main activity. What should I change or add to this code or am I taking the wrong approach for what I am trying to achieve
Access.java
public class Access extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.access);
initializePaging();
}
private void initializePaging() {
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, AccessForgotPassword.class.getName()));
fragments.add(Fragment.instantiate(this, AccessLogin.class.getName()));
fragments.add(Fragment.instantiate(this, AccessRegister.class.getName()));
PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager accessViewPager = (ViewPager) findViewById(R.id.accessViewPager);
accessViewPager.setAdapter(mPagerAdapter);
}
}
access.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/accessMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".access.Access">
<android.support.v4.view.ViewPager
android:id="#+id/accessViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
AccessForgotPassword.java
public class AccessForgotPassword extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_forgot_password, container, false);
}
}
access_forgot_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessForgotPasswordMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000">
</LinearLayout>
AccessLogin.java
public class AccessLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.access_login, container, false);
}
}
access_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessLoginMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
</LinearLayout>
AccessRegister.java
public class AccessRegister extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.infalte(R.layout.access_register, container, false);
}
}
access_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accessRegisterMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0000FF">
</LinearLayout>
Set
accessViewPager.setCurrentItem(1);
Try adding accessViewPager.setCurrentItem(1) in end of onCreate()
Try to set accessViewPager.setCurrentItem(1); in the onCreate() of your activity.
Just after setting adapter you can set manually set the second fragment to view pager
accessViewPager.setCurrentItem(1); // 0= ForgotPassword, 1=LoginScreen,...so on
I have a fragment and I am inflating it like below, but it is giving runtime error:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View jobsView = inflater.inflate(R.layout.fragment_job_information, container, false);
textView = (TextView) jobsView.findViewById(R.id.textView);
if (jobInfo == null) {
EmployerHistory jobsQuery=new EmployerHistory();
jobInfo = jobsQuery.getJobInfo(employerName, position);
}
textView.setText(jobInfo.get("jobName").toString());
return jobsView;
}
Its corresponding layout is:
<fragment 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" android:id="#+id/jobInfoFragment">
<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="com.example.ankit.job_depot.employer.view.JobInformation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</fragment>
Actually I am already on a fragment with listview and when user clicks on an element of listview that fragment needs to get changed by a new fragment, in my new fragment I have:
l
istViewJobHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JobInformation newFragment = new JobInformation();
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.listViewJobHistory, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
I am getting the following exception:
07-28 18:49:39.681 14474-14474/com.example.ankit.job_depot
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ankit.job_depot, PID: 14474
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.example.ankit.job_depot.employer.view.JobInformation.onCreateView(JobInformation.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
I think your main Activity should extend FragmentActivity
import android.support.v4.app.FragmentActivity;
I'm trying to launch a list fragment but it only seems to be working on tablets. When I run my app on phones the app crashes. Does anyone know how to resolve this issue? Code are below.
Error
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 (com.apptacularapps.exitsexpertlondonlite:id/master_container) for fragment FragmentMainList{b76424 #1 id=0x7f0c0050}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
}
}
activity_main.xml
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list_main"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentMainList"
tools:layout="#android:layout/list_content"/>
activity_main.xml (sw600dp)
<LinearLayout 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"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/master_container"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="#+id/detail_container"/>
</LinearLayout>
FragmentMainList.java
public class FragmentMainList extends android.support.v4.app.Fragment {
ListView list_main;
String[] listContent = {
"Item 1",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
return v;
}
}
it's because you have this line
transaction.replace(R.id.master_container, newFragment);
and you don't have any layout inside activity_main.xml that has the same id as master_container
I just following a tutorial and don't understand why it's showing error. Here is my codes..
FragmentManager fManager;
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_test);
fManager = getFragmentManager();
MakeTransaction();
}
public void MakeTransaction(){
MyFragmentClass mfrag = new MyFragmentClass();
FragmentTransaction trans = fManager.beginTransaction();
trans.add(R.id.view_group,mfrag,"G");
}
On the line "trans.add(R.id.view_group,mfrag,"G");" I am getting error...
The error is add() in fragment transaction cannot be applied ..
here is the fragment class code##
public class MyFragmentClass extends Fragment {
Button ph;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_setup, container,false);
}
here is the main xml code where i have two fragment..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_group"
android:orientation="horizontal">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_setup"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_display"
/>
</LinearLayout>
</LinearLayout>
The error is add() in fragment transaction cannot be applied ..
this is usually because of a mismatch between the Fragment imports of your Fragment sublcass and in the Activity. In your case you are using getFragmentManager, and MyFragmentClass is probably a subclass of Fragment from the support library. In your Activity use getSupportFragmentManager() instead of getFragmentManager(). You'll have to extend one between FragmentActivity, AppCompatActivity or ActionBarActivity for this purpose