简体   繁体   中英

Android Studio won't compile because of getText()

This is my problem, guys:

  1. I have a list of objects (a title and a header each that must be clicked) on MainActivity
  2. On clicking either, they are to be sent to SecondActivity.
  3. Everything works fine except getText(), which is the only method I know to get the title or header text.
  4. On compile I get this: "error: cannot find symbol method getText()"
  5. I swear I've searched everywhere for answers and tested all of them.
  6. Please bear with me. I'm an Android newbie. I will appreciate your help.

This is the relevant code I've written:

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
public void onButtonClick(View v){
 Intent myIntent = new Intent(getBaseContext(),SecondActivity.class);
 startActivity(myIntent);
}

public void onTitleOrHeaderClick(View v)
{
    int    length;
    Intent intentToSecondActivity = new Intent(getBaseContext(),SecondActivity.class);
String tipHeader = v.getText();   /* for some reason getText() isn't recognized by the emulator */

intentToSecondActivity.putExtra("EXTRA_SESSION_ID","GOING to SecondActivity");

length = this.itemArray.tipObject.size();
for (int idx = 0; idx < length; idx++)
{
  if(this.itemArray.tipObject.get(idx).get(1) == v.getText())
    intentToSecondActivity.putExtra("EXTRA_SESSION_ID",v.getText());
}
}
}

cast v to Textview( the class type of title and header):

public void onTitleOrHeaderClick(View v){
    int    length;
    TextView view=(TextView) v
    Intent intentToSecondActivity = new Intent(getBaseContext(),SecondActivity.class);
    String tipHeader = view.getText();   /* for some reason getText() is recognized now */
    intentToSecondActivity.putExtra("EXTRA_SESSION_ID","GOING to SecondActivity");
    length = this.itemArray.tipObject.size();
    for (int idx = 0; idx < length; idx++){
          if(this.itemArray.tipObject.get(idx).get(1) == view.getText())
              intentToSecondActivity.putExtra("EXTRA_SESSION_ID",view.getText());
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM