why am I getting "cannot find symbol class textview" ?

  • Replies:25
tim guggenheim
  • Forum posts: 1

Apr 5, 2016, 2:57:17 AM via Website

I am a newbie and want to create a quick app to see "if I can do it"
I appreciate anyone who would waste their time w me~~
I am getting "cannot find symbol class textview" ?
Just trying to make my phone display a number when the only button I have is clicked.

my code:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End the World"
android:id="@+id/endbutton"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="47dp"
android:onClick="myendMethod"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a dumb test"
android:id="@+id/textView1"
android:textColor="#ad0b0b"
/>

public void myendmethod(View v)
{
int count=7;
TextView textView = (TextView)findViewById(R.id.textView1);

}

Reply
ConzT
  • Forum posts: 6

Apr 5, 2016, 11:26:27 AM via Website

Hi,

First of all you need a start Tag for each defined TextView,Button,....

For example:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End the World"
android:id="@+id/endbutton"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="47dp"
android:onClick="myendMethod"
/>

or

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a dumb test"
android:id="@+id/textView1"
android:textColor="#ad0b0b"
/>

do you have an onCreate method?

If no, you must implement it in your Activity!

example:

@Override

protected void onCreate(Bundle savedInstanceState{
super.onCreate(savedInstanceState);
setContentView(R.layout.name_of_your_xml_file_which_has_the_layout); 
TextView textView;
textView = (TextView)findViewById(R.id.id_of_the_textView_as_given_in_your_layout.xml);
textView.setTextSize(40);
textView.setText("TEST");

}

If you missed that out i would propose that you first read a book on android development or check out developer.android. com /guide/ index.html

Vladimir S.

Reply
Ashish Tripathi
  • Forum posts: 211

Apr 6, 2016, 6:42:46 AM via Website

// assuming that this is a button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End the World"
android:id="@+id/endbutton"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="47dp"
android:onClick="myendMethod"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a dumb test"
android:id="@+id/textView1"
android:textColor="#ad0b0b"
/>

public void onCreate(Bundle b){

setContentView(R.layout.your xml name);

// Initialize the TextView in Class not in the method

TextView textView = (TextView)findViewById(R.id.textView1);

}

public void myendmethod(View v)
{
int count=7;
textView.setText(String.Valueof(count));

}

Try this one

Reply
TBguitar
  • Forum posts: 12

Apr 8, 2016, 3:21:04 AM via Website

The oncreate routine programmed itself during the visual development, so I added your code and get this error:FRUSTRATING:
Error:(24, 9) error: cannot find symbol class TextView

my code below:

package com.example.theboss.having_fun;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setContentView(R.layout.content_main);
    TextView textView;
    textView = (TextView)findViewById(R.id.textView1);
    textView.setTextSize(40);
    textView.setText("TEST");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public void myendmethod(View v)
{
    int count=7;
    TextView textView = (TextView)findViewById(R.id.textView1);

}

}

Reply
TBguitar
  • Forum posts: 12

Apr 10, 2016, 10:06:33 PM via Website

any clues?
BTW, I had to log in via google, the change pwd feature for this site DOES NOT work, it's useless :(

— modified on Apr 10, 2016, 10:07:39 PM

Reply
Vladimir S.
  • Forum posts: 266

Apr 10, 2016, 10:40:00 PM via Website

You need to add import android.widget.TextView; into import section.

— modified on Apr 10, 2016, 10:40:17 PM

Reply
TBguitar
  • Forum posts: 12

Apr 12, 2016, 2:03:30 AM via Website

Thanks for the reply. I got frustrated and went back to my orig project, added your line from above
" import android.widget.TextView;" and that took care of that error.
When I click on the "endtheworld" button, nothing happens, but at least I get NO ERRORS!
Can you advise?

public void endtheworld(View view) {
// Do something in response to button click
int zcount=7;
TextView textView = (TextView)findViewById(R.id.textView1);
System.out.println("FROG");

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push to end the world"
android:id="@+id/endbutton"
android:layout_marginLeft="38dp"
android:layout_marginStart="38dp"
android:background="#ccb3b3"
android:layout_below="@+id/endbutton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:onClick="endtheworld"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Frogs are green"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/endbutton"
android:id="@+id/textView1"
/>

Vladimir S.

Reply
Ashish Tripathi
  • Forum posts: 211

Apr 12, 2016, 6:53:58 AM via Website

public void myendmethod(View v)
{
int count=7;
TextView textView = (TextView)findViewById(R.id.textView1);

}

Coz you are not doing any thing on button click. If you want to check may be you can try

myendmethod should be defined in xml like

android:onClick="myendmethod"

public void myendmethod(View v)
{
int count=7;
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(String.valueof(count));
}

}

Vladimir S.

Reply
TBguitar
  • Forum posts: 12

Apr 13, 2016, 2:21:28 AM via Website

Error now is:
Error:(50, 32) error: cannot find symbol method valueof(int)
I thought I was having the button do something, that is: System.out.println("FROG");
Print the word "frog" but although I dont see output in the log I see:
04-12 20:32:11.292 2302-2302/com.example.theboss.myapplication_apr I/System.out: FROG

— modified on Apr 13, 2016, 2:33:16 AM

Reply
Ashish Tripathi
  • Forum posts: 211

Apr 13, 2016, 6:50:33 AM via Website

setText(String.valueOf(count));

or

setText(""+count);

try this

Reply
TBguitar
  • Forum posts: 12

Apr 14, 2016, 2:04:05 AM via Website

Your tip worked. But that was not my problem. When I output, my button is hiding where the count gets displayed. I need to move the button, but my button has the id of textView1!

Reply
Ashish Tripathi
  • Forum posts: 211

Apr 14, 2016, 6:52:10 AM via Website

Your Button id is endbutton so on the method as per by xml you mentioned in above post i.e. endtheworld

// put this line in onCrate

endbutton = (Button)findViewById(R.id.endbutton);

// As i did not get any thing which hide you button but still it is hiding use this

public void endtheworld(View v ){
endbutton.setVisibility(View.Visisble);
endbutton.setText(""+count);
}

Reply
TBguitar
  • Forum posts: 12

Apr 23, 2016, 2:09:11 PM via Website

Yes, got it! thanks for the help, still a newbie here and I have this test app successfully ported to my Moto E phone, all in fun.

Another quest, I am trying to display a counting loop iterating and want to display in a textview, should this work? I tried to slow it down using a nested loop but I do not get the continual numbers displaying, rather it just displays 199 whens its done
my code:

public void mynumbers(View v) {
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setVisibility(View.VISIBLE);

    for(int i=0; i < 200; i++)
    {
        for(int j=0; j < 1150200; j++) {}
            String strI = String.valueOf(i);
            tv2.setText(strI);
         }
        }

— modified on Apr 23, 2016, 2:31:44 PM

Reply
Vladimir S.
  • Forum posts: 266

Apr 23, 2016, 3:15:13 PM via Website

In your simple app you can use Thread.sleep(50) before tv2.setText(strI);, but a better idea is to use CountDownTimer for pause.

— modified on Apr 23, 2016, 3:17:17 PM

Reply
TBguitar
  • Forum posts: 12

Apr 23, 2016, 5:34:51 PM via Website

the thread.sleep gives me:
Error:(118, 25) error: unreported exception InterruptedException; must be caught or declared to be thrown

Then I found:
try{ Thread.sleep(10); }catch(InterruptedException e){ }
no errors, but there was no continual display of counting, just went right to 199 ;(

— modified on Apr 23, 2016, 5:47:29 PM

Reply
Vladimir S.
  • Forum posts: 266

Apr 23, 2016, 6:16:23 PM via Website

Is your code similar?

String strI;
for(int i=0; i < 200; i++)
{
try { Thread.sleep(10);
} catch(InterruptedException e){ }
strI = String.valueOf(i);
tv2.setText(strI);
}

Reply
TBguitar
  • Forum posts: 12

Apr 23, 2016, 6:45:17 PM via Website

Yes

for (int i = 0; i < 200; i++) {
String strI = String.valueOf(i);
try{ Thread.sleep(10); }catch(InterruptedException e){ }
tv2.setText(strI);

    }

Reply
Vladimir S.
  • Forum posts: 266

Apr 23, 2016, 7:45:44 PM via Website

10 ms is very little time, so eyes can't see changes. Try:
.........
for (int i=1; i < 30; i++)
.........
try{ Thread.sleep(100); }catch(InterruptedException e){ }
..........

Reply
TBguitar
  • Forum posts: 12

Apr 24, 2016, 2:40:42 PM via Website

Nope, still nothing appears until it's done counting.
I had to use 30 because at 100, the app timeouts says "not responding" do I want to wait or close it

— modified on Apr 24, 2016, 2:42:55 PM

Reply
Vladimir S.
  • Forum posts: 266

Apr 24, 2016, 3:04:04 PM via Website

Anyway thread.sleep() is not a best technique. Use CountDownTimer instead, for example:

String strI;
int tick = 0;

CountDownTimer timer = new CountDownTimer(2000, 25) {

        @Override
        public void onTick(long l) {
               tick++
               strI = String.valueOf(tick);
               tv2.setText(strI);
        }

        @Override
        public void onFinish() {
        }
    };

Reply
TBguitar
  • Forum posts: 12

Apr 24, 2016, 7:09:28 PM via Website

I get this now
Error:(103, 17) error: local variable tick is accessed from within inner class; needs to be declared final

I pasted in your code.

Reply
Vladimir S.
  • Forum posts: 266

Apr 24, 2016, 9:23:10 PM via Website

Make variable "tick" global or put declaration into timer's brackets. ;)

Reply
TBguitar
  • Forum posts: 12

Apr 25, 2016, 2:10:53 AM via Website

the global route seems quite complicated, so I did this, putting variables within CountDownTimer's brackets, now no numbers show. :( I appreciate the help you are providing :)

public void mynumbers(View v) {
// TextView tv2 = (TextView) findViewById(R.id.textView2);
// tv2.setVisibility(View.VISIBLE);

   // String strI;
  //  int tick = 0;

    CountDownTimer timer = new CountDownTimer(2000, 25) {
        int tick = 0;
        String strI;
        TextView tv2 = (TextView) findViewById(R.id.textView2);

        @Override
        public void onTick(long l) {
            tick++;
           strI = String.valueOf(tick);
            tv2.setText(strI);
        }

        @Override
        public void onFinish() {
        }
    };
}

Reply
Vladimir S.
  • Forum posts: 266

Apr 25, 2016, 2:18:07 PM via Website

This code should work:

public class MainActivity extends Activity {

    TextView tv2;
    int tick;
    String strI;

    CountDownTimer timer = new CountDownTimer(5000, 50) {

        @Override
        public void onTick(long l) {
            tick++;
            strI = Integer.toString(tick);
            tv2.setText(strI);
        }

        @Override
        public void onFinish() {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv2 = (TextView) findViewById(R.id.textView2);
    }

    public void mynumbers(View v) {
        tick = 1;
        timer.start();
    }
}

Reply
TBguitar
  • Forum posts: 12

Apr 26, 2016, 10:42:04 PM via Website

YES!!
working now. and thank you.
One thing, my line here is a bit different then yours, why?

public class MainActivity extends AppCompatActivity {

Reply
Vladimir S.
  • Forum posts: 266

Apr 26, 2016, 11:33:58 PM via Website

AppCompatActivity is a safe to use backward compatibility class. It brings a single consistent ActionBar for all devices starting with API Level 7 (Android 2.1) and above. Android Studio uses AppCompatActivity by default.

Reply