I am getting a cannot resolve symbol error on CrimeFragment and I don't know why. I am a beginner so I'm not sure how to fix this. Here is the code for CrimeActivity.java
package com.bignerdranch.android.criminalintent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class CrimeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
Seems like you aren't importing your class CrimeFragment
Make sure that CrimeFragment.java and Crime.java are in the right packages. Select "Packages" view in the top-left corner. If these files hang around separately at the bottom, drag and drop them into the "com.(..).criminalintent" package.
Related
Hello I just want to switch from a fragment to another using a button. Nothing difficult... the code works well but I have spent a lot of time because Android goesto error if I hook frameLayout with autocompile.
I'm explaining better:
package com.example.fragcookbook;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
FragmentOne mFragmentOne;
FragmentTwo mFragmentTwo;
int showingFragment=0;
#Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
mFragmentOne = new FragmentOne();
mFragmentTwo = new FragmentTwo();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout, mFragmentOne);
fragmentTransaction.commit();
showingFragment=1;
}
public void switchFragment(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (showingFragment==1) {
fragmentTransaction.replace(R.id.frameLayout, mFragmentTwo);
showingFragment = 2;
} else {
fragmentTransaction.replace(R.id.frameLayout, mFragmentOne);
showingFragment=1;
}
fragmentTransaction.commit();
}
In all rows where you see R.id.frameLayout i have to handly write frameLayout..... Android colors it red but It works...
But if i choose the resource with autocompile...
...when i run app Android gives me this error....
Why? is not best practice use autocompile?
Thanks in advance
Try this may will help you.Go to "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.
Or
Restar you android studio and emulator
When I call fragment from activity then fragment calling successfully but some part of previous activity is on the top of in fragment, anyone help to correct the code
package com.example.newproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import static android.view.View.GONE;
public class ThankYouPageActivity extends AppCompatActivity {
Button thank_continue_btn,thank_track_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thank_you_page);
getSupportActionBar().setTitle("Confirmation");
thank_continue_btn = findViewById(R.id.thank_continue_btn);
thank_track_btn = findViewById(R.id.thank_track_btn);
findViewById(R.id.thank_continue_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
HomeFragment fragment = new HomeFragment();
fm.beginTransaction().replace(R.id.thanku_container, fragment).addToBackStack(null).commit();
set background to white in the root tag of your fragment's xml
background = "#FFFFFF"
The container of your Fragment must be overlapped by your Activity layouts. Please attach the layouts of your Activity and Fragment so that I can help you find your issue.
I had same issues , i fixed it by removing previous fragment while setting new fragment in oncreateview
container.removeAllViews(); before replacing the fragment
R.id.thanku_container - I think it is FrameLayout?
Make it full match_parent. Or set background color like activity.
I am very new to Android programming and I was wondering how I am able to fix this error that I'm getting. I have scowwered the internet searching for solutions on how to compensate for the fact that I have api 15 instead of the required 17 for my FragmentTransaction. I tried importing the support.v4.app.FragmentTransaction but still no luck here is the code:
package com.hfad.workout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
private long workoutId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(savedInstanceState != null)
{
workoutId = savedInstanceState.getLong("workoutId");
}
android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
StopwatchFragment stopwatchFragment = new StopwatchFragment();
ft.replace(R.id.stopwatch_container, stopwatchFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
}
Any reason as to why the support.v4.app might not be working? Is there a work around to this? Any help is greatly appreciated :D
You have use "import android.app.Fragment" for your fragment.That's why your support.v4.appnot working here.
Try to import android.support.v4.app.Fragment for using android.support.v4.app.FragmentTransaction.
You can try to chaging from
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
to
import android.support.v4.app;
public class WorkoutDetailFragment extends Fragment {
I have a list fragment from where when any list item is clicked, I want to move to another list fragement.
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Schedule extends ListFragment{
....
....
public void onListItemClick(ListView l, View v, int position, long id) {
....
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DailyList fragment = new DailyList();
fragmentTransaction.replace(R.id.list_fragment, fragment);
fragmentTransaction.commit();
}
This is the new class that I am trying to move to.
public class DailyList extends ListFragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
final View theInflatedView = inflater.inflate(R.layout.day, container, false);
return theInflatedView;
}
....
....
....
}
I tried using v4 android support libraries. but the replace call is giving me this error "The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, DailyList)".
I read few other answers that asked to use v4. That is what I am doing. But still getting error. Please help me out here. Thanks!
I think you are mixing newer fragment support types with support classes. Replace these lines:
import android.app.Fragment;
import android.app.FragmentManager;
with:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Then extend
android.support.v4.app.FragmentActivity
instead of
Activity
and change calls like
getFragmentManager()
with
getSupportFragmentManager()
and similarly for other methods that you might be using.
So I've decide to go back into android developing after dropping it for a bit. I Restarted making an old project in android studio I ran into a issue where I'm getting "cannot resolve symbol fragmentcontainer" and I'm sure it was working last time.
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add fragment to the activity
FragmentManager fm = getSupportFragmentManager();
// give fragment to manage
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new HomeFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
It looks like your Activity layout R.layout.activity_main does not contain a view with id fragmentContainer. If that's not the issue, check this related question: Android Studio cannot resolve symbol but code executes correctly.
just using "fragment_container" not "android.R.id.fragment_container" works for me...here is the detail
getFragmentManager().beginTransaction()
.replace(fragment_container, new SettingsFragment())
.commit();
I think you can use
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commitNow(); instead of using R.id.fragmentContainer to create fragment in an activity.