I´ve been looking for answers, and have tried a lot of things, but nothing is working...
So, Im doing what I think is a simple app (I just started learning)
So I have an activity sending a couple of string to another where a couple of TextViews should chnage ther values to the new String, but this is not happening. Here you have the code:
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Intent intent = getIntent();
String mail = intent.getStringExtra(MainActivity.MAIL);
String password = intent.getStringExtra(MainActivity.PASSWORD);
setContentView(R.layout.fragment_login);
TextView displayMailSetter = (TextView) findViewById(R.id.mailView);
displayMailSetter.setText(mail);
displayMailSetter.postInvalidate();
TextView displayPasswordSetter = (TextView)findViewById(R.id.passwordView);
displayPasswordSetter.setText(password);
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
}
}
Try taking out these lines of code:
displayMailSetter.postInvalidate();
// Don't take out your displayPasswordSetter initilization and setText
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
Change these lines of code:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
to
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
and take out the origional (right under the intents).
setContentView(R.layout.fragment_login);
The problem was that you were setting the layout to 3 different layouts. The first and third were the same layout, but didn't have the TextViews. The first and third are unnessecary, you should only set the layout once. Once you set it to fragment_login, the layout had the TextViews, set the text of them, but the layout was then changed to a layout without the TextViews.
Related
I want to rename different things (Buttons and Textviews) in a app from the code.
But because those are in a fragment for a Tabview this wont work and will lead to a crash:
someTextview.setText("some Text");
I have tried this Stack Overflow solution and this one, but both solutions didnt work.
Is there something else that i could try?
In your onCreateView() function first inflate the view like this
View view = inflater.inflate(R.layout.xyz, container, false);
then
TextView txtview = (TextView) findViewById(R.id.ANINRHinten);
txtview.setText("Something");
It will solve your problem.
I tried to put the .setText inside the onCreate main function, as well as the functions in the fragment class.
I didn't get any errors at all. If the setText is in the main onCreate function it compiles and opens on the simulator but crashes before doing anything.
When i tried to use it in the fragment function onCreateView, the app functions without any crashes or errors, but also the setText had no effect.
I have done the whole thing like you said from the start:
TextView txtview;
//Inside void onCreate:
txtview = (TextView) findViewById(R.id.ANINRHinten);
txtview.setText("Something");
Did you define your textView?
If you didn't you can define it like below :
TextView textView = (TextView) findViewById(R.id.text_view_id);
textView.setText("")
or if you use binding define it and use it like :
binding.textView.setText("")
I started programing with Java and Android Studio a week ago, so feel free to tell me what else i can improve.
private TextView waitForConn;
private TabLayout maintab;
private ViewPager page;
private TabItem tablicht, tabadmin;
public PagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*For future implementations of BLE
setContentView(R.layout.activity_main);
waitForConn = (TextView) findViewById(R.id.wait_for_connection);
waitForConn.setText("Suche Schnapsiinator...");
waitForConn.setTextColor(Color.GREEN);
waitForConn.setText("Verbinden...");
waitForConn.setText("Verbunden");
*/
setContentView(R.layout.main_menu);
maintab = (TabLayout) findViewById(R.id.tabLayout);
tablicht = (TabItem) findViewById(R.id.licht_tab);
tabadmin = (TabItem) findViewById(R.id.admin_tab);
page = findViewById(R.id.viewpager_lichtadmin);
pagerAdapter = new PageAdapter(getSupportFragmentManager(), maintab.getTabCount());
page.setAdapter(pagerAdapter);
//Compiler says "error: incompatible types: int cannot be converted to byte[]"
View view = Inflater.inflate(R.layout.main_menu, page, false);
//at: ^
maintab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
page.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0){
pagerAdapter.notifyDataSetChanged();
}else{
pagerAdapter.notifyDataSetChanged();
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
page.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(maintab));
}
In what function is that code? What error have you get?
have you take the id of that textview? we need more information.
Btw, if you create a variable like this on your onCreateView
button = findViewById(R.id.idOfTheButton)
then you can use it on youre OnViewCreated like you wrote in the question.
button.setText("text")
I am following Udacity's course where at last during completion I met with this error.
You can also check the Udacity's code by clicking here
and my layout files 1 & 2.
**
Thanks in advance.
**
com.thelazyprogrammer.harshit.mycustomapps.ui.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6998)
This error is pointing to setContentView(R.layout.activity_main);
But don't know what is missing.
My code:-
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener
{
// Variable to store the values of list index of selected image.
private int headIndex,
bodyIndex,
legIndex;
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/*<---THE ABOVE ERROR POINTING BELOW LINE'S CODE--->*/
setContentView(R.layout.activity_main);
/*<------------------------------------------------>*/
/*Intent intent = new Intent(this, AndroidMeActivity.class);
startActivity(intent);*/
if(findViewById(R.id.android_me_linear_layout) != null)
{
mTwoPane=true;
// Setting grid view into two columns.
GridView gridView = (GridView) findViewById(R.id.images_grid_view);
gridView.setNumColumns(2);
// Getting rid of the button in the tablet mode.
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setVisibility(View.GONE);
if(savedInstanceState!=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
BodyClassFragment headFragment, bodyFragment, legFragment;
// Creating new head, body, leg.
headFragment = new BodyClassFragment();
headFragment.setImageIds(AndroidImageAssets.getHeads());
bodyFragment = new BodyClassFragment();
bodyFragment.setImageIds(AndroidImageAssets.getBodies());
legFragment = new BodyClassFragment();
legFragment.setImageIds(AndroidImageAssets.getLegs());
// Adding fragment to it's container.
fragmentManager.beginTransaction()
.add(R.id.head_container, headFragment)
.add(R.id.body_container, bodyFragment)
.add(R.id.leg_container, legFragment)
.commit();
}
}
else
{
mTwoPane=false;
}
}
I think you have to learn the basic of display size from Google Docs.
In the XML file for my layout I added a fragment.
The layout for my fragment is a LinearLayout and it contains a text view.
I want to get a reference to the LinearLayout object in my fragment class, but despite trying everything I can think of or find online, it is always null.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.text);
layout = (LinearLayout) getActivity().findViewById(R.id.layout);
if(layout == null){
text.setText("null");
}
}
This code is in my fragment class and results in the TextView's text being set to "null" since the layout is null. How could this be since I am trying to get the references to the layout and the textview in the exact same way?
onCreate in activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
}
Method 2
I tried getting a reference to the LinearLayout in my activity. Here is the activity's onCreate with this attempt.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
TextView tv = (TextView) findViewById(R.id.text);
if(ll == null){
tv.setText("null");
}
}
This also changed the text to "null."
The problem is fixed in both cases by saying layout = (LinearLayout) getView() in the fragment class or layout = (LinearLayout) fragment.getView() in the activity. This doesn't answer why what I was doing didn't work (which could be helpful) but this solves the problem.
when I extends from Activity, I modify the fragment_main and find controls like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
MyButton = (Button) findViewById(R.id.button1);
}
but when I do this when extends from from ActionBarActivity gives me an error, so to do it work I have to comment:
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
and set activity_main to fragment_main
setContentView(R.layout.fragment_main);
but I don't believe this be correct...
You should not get the button view on your activity, however it sides in a fragment. You can get it on your fragment such as PlaceholderFragment like you do on your activity. If needed, you should know something about how to interactive between Activity and Fragment. the link http://developer.android.com/guide/components/fragments.html may help you.
I am learning to develop apps for android from a book and after following the instructions I end up with the following.
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
I get wavy red underline under the dataView1 in findViewById(). Eclipse informs that dataView1 cannot be resolved or is not a field.
The complete code look like this and i get wavy red underline under all dataView. It says nothing about this error in the book.
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
Here the MainActivity
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setStartUpWorldValue();
setStartUpScreenText();
}
protected void setStartUpWorldValue(){
earth.setPlanetColonies(1);
earth.setPlanetMilitary(1);
earth.setColonyImmigration(1000);
earth.setBaseProtection(100);
earth.turnForceFieldOn();
}
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
It seems you built your views inside fragment_main.xml and not activity_main.xml.
When you first create a new android project, you have these files which are automatically created and opened:
Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml file. Finally, you tried to do a basically event with this view inside your Activity, something like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Using layout activity_main.xml
// You try to set a simple text on the view (TextView) previously added
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Simple Text"); // And you get an error here!
/*
* You do an fragment transaction to add PlaceholderFragment Fragment
* on screen - this below snippnet is automatically created.
*/
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..
Solution: Move all your stuff inside onCreateView method into the Fragment class.
Call views and do something in the related fragment and not the parent activity.
Or you can do all inside the activity, by removing these lines which to not add a fragment on your UI:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
This above create and add a fragment "above" your activity layout. And then, setting the right layout here:
setContentView(R.layout.fragment_main);