0

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?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
nomedia
  • 25
  • 5

5 Answers5

0

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")
0

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");
nomedia
  • 25
  • 5
0

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("")
GolnazTorabi
  • 50
  • 1
  • 3
0

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'm trying but i always get the error: 'error: incompatible types: int cannot be converted to byte[] View view = Inflater.inflate(R.layout.main_menu, page, false);' Maybe i am using the wrong layout... – nomedia Dec 28 '20 at 16:30
  • can you paste your whole `onCreateView()` function? – Aayush Chaudhary Dec 29 '20 at 03:25
  • I got it now. I just used the wrong coCreateView function. I used the one of the main activity instead of the fragment onCreateView function. Thanks. – nomedia Dec 31 '20 at 12:25
0

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));
}
nomedia
  • 25
  • 5