I am getting errors on the R variables. The variables are R.id.drawer_layout, R.id.layout, and R.id.main. There is a red squiggly line under the words after id. These are all errors from R. Is there something wrong with my layout? I'm just beginning android programming and this is coming from github.
http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/
^This is where the tutorial and code is from.
package com.anshikka.datagushalpha;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] data ={"one","two","three"};
final String[] fragments ={
"com.anshikka.datagushalpha.FragmentOne",
"com.anshikka.datagushalpha.FragmentTwo",
"com.anshikka.datagushalpha.FragmentThree"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
#SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter adapter = new ArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
}
Import your R,
is something like
import com.yourproject.R;
Look into you imports and if you find "android.R" delete it. Finally, press Ctrl + Shift + O to import your project's R (using Eclipse as IDE) or just manually add it:
import com.anshikka.R
After that you should see com.anshikka.R on your imports at the top of the file.
Check if you have two main.java classes. I had this problem a couple of days ago when I imported a project into a current project. I had two main classes and after I deleted one the R error disappeared. Also make sure you imported the right R class. If after you imported the R class you get a redline under .activity_main you imported the wrong R class
I suppose those variables don't exist, since the red line only appears under the words after "id".
If you haven't copied the file activity_main.xml provided in the link you mentioned, which defines "#+id/drawer_layout", "#+id/"main" and "#+id/drawer", into your project's res/layout folder, the corresponding variables weren't generated.
Related
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 developing an Android app that has a little bit confusing navigation structure which leads me to my problem. There are two ways to navigate through the app. First is the BottomNav and second is a TabMenu. I thought about working with fragments that replace each other, so I came up with the following structure:
1. BottomNav#1
- TabMenu#1
- Tabmenu#2
- TabMenu#3
- ...
2. BottomNav#2
- TabMenu#4
- Tabmenu#5
- TabMenu#6
- ...
... and so on.
The problem I am facing is that when I navigate from BottomNav#1 to BottomNav#2 and back again there is a blank screen that doesn't show the content of the actual fragment:
When I open the app
After clicking on BottomNav#2 and then back to BottomNav#1 the fragment seems to be blank
My guess is that I somehow have a problem with my fragmentTransaction.replace(); as it seems like the fragment doesn't get loaded again? I am kind of new to this and really tried to find an answer online but this is a bit specific why I guess I didn't find anything.
This is my MainActivity:
package com.example.XXXX;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private FrameLayout mMainFrame;
private AusweisFragment ausweisFragment;
private SpendenFragment spendenFragment;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ausweisFragment = new AusweisFragment();
spendenFragment = new SpendenFragment();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_ausweis:
setFragment(ausweisFragment);
return true;
case R.id.navigation_spenden:
setFragment(spendenFragment);
return true;
}
return false;
}
};
private void setFragment(Fragment fragment)
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
This should lead to e.g. the fragment called "ausweis" by clicking on on BottomNav#1 (switch case):
package com.example.XXXXXXX;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AusweisFragment extends Fragment {
private View rootView;
private AusweisPageAdapter ausweisPageAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
public AusweisFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return rootView;
}
}
After that I would like to call another fragment called "spenden" which is exactly the same as the fragment called "ausweis" but with different named tabs, so I think it is not bother you with more code.
edit: I missed about writing where the "content" I wrote about gets from. For a first try and proof that the fragments change, I hardcoded a phrase like "Ausweis" into the XML which is connected to my fragment java class.
Maybe one of you has an idea to that problem. I think it has something to do with my onCreateView in one of the fragments, but I have no close clue.
Hopefuly I didn't miss an important detail. I am very grateful for any kind of help. Thanks a lot in advance.
The answer to my problem was to use a child-parent relation between the different fragments. With the click on BottomNav#1 I am inflating an fragment which inflates a new fragment inside itself. That was the key problem. In my code I handled this as a second "normal" getFragmentManager();.
The answer is to use getChildFragmentManager(); for the nested fragment instead. Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
tabLayout1 = rootView.findViewById(R.id.tablayoutAusweis);
viewPager = rootView.findViewById(R.id.viewPagerAusweis);
ausweisPageAdapter = new AusweisPageAdapter(getChildFragmentManager(), tabLayout1.getTabCount());
viewPager.setAdapter(ausweisPageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
...
...
Hopefuly this will help some people when they face the same problem with nested fragments.
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.
Can anyone tell me why this isnt working? I havent changed this file since it worked last but now I get the error below:
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private final static int NUM_PAGES = 2;
private ViewPager mPager;
private ScreenSlidePagerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(2);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment frag = null;
switch (i) {
case 0:
frag = new PageOneFragment();
break;
case 1:
frag = new PageTwoFragment();
break;
}
return frag;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
The error is on "frag = new PageTwoFragment();" which states "Type mismatch: cannot convert from PageTwoFragment to Fragment".
Maybe I should create two projects from now on, last good version and then current working project. Is this something other people do?
Thanks
The Problem is, that you are mixing Android Fragments how there were introduced in API11 and Fragments from android support library.
You have to use one or the other, but not both.
Change your imports to
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
And change getFragmentManager() to getSupportFragmentManager().
And it will work using the support lib.
Calling mPager.setCurrentItem(2); with a pager of 2 Fragments crashs, though.
Only 0 and 1 are valid values in your case.
You imported the wrong Fragment.
You need to import android.support.v4.Fragment
Furthermore, your adapter only handles 2 Fragments. Therefore calling
ViewPager.setCurrentItem(2);
will cause problems, since the index for the first fragment is 0.
I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html