I have passed a value through putExtra from first screen and fetched by getStringExtra method to second screen. To confirm that I have received the value, I passed it as TextView on second screen and I was able to get it.
Now I am passing this value to url, but android studio is showing redline under it. During mouse rollover it shows following error.
Cannot resolve symbol 'cropcategoryname'
The code is as follows.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
I believe it is a simple problem but I've been stuck on it for a while.
String cropcategoryname = i.getStringExtra("cropCategoryName");
This is a local variable in the onCreate() method.
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
This is an instance field in the activity class. The local variable in onCreate() does not exist when this code is executed.
I suggest that you learn about variable scope and the activity lifecycle. Since the value for url relies on a value that is only available in onCreate(), you must initialize url in that method, not inline with its field declaration:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
}
private String url;
Try this:
Use this in your CropnameActivity before onCreate:
private static final String EXTRA_NAME = "EXTRA_NAME";
private String cropcategoryname;
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=";
private static Intent createIntent(Context context, String cropCategoryName) {
Intent intent = new Intent(context, CropnameActivity .class);
intent.putExtra(EXTRA_NAME, cropCategoryName);
return intent;
}
public static void startActivity(Activity activity, String cropCategoryName) {
activity.startActivity(createIntent(activity, cropCategoryName));
}
from your MainActivity do this:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String cropCategoryName =((TextView)view.findViewById(R.id.cropCategoryName)).getText().toString();
Toast.makeText(getApplicationContext(),cropCategoryName,Toast.LENGTH_SHORT).show();
CropnameActivity.startActivity(MainActivity.this, cropCategoryName);
});
then retrieve your String as such:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
cropcategoryname = getIntent().getStringExtra(EXTRA_NAME);
if(cropcategoryname != null) txtName.setText(cropcategoryname);
}
After that you can use the url String as such:
if(cropcategoryname != null) {
String finalUrl = url + cropcategoryname;
}
Hope it helps!!!
Simply initialize the cropcategoryname string variable above the onCreate method.
String cropcategoryname="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
Related
I'm trying to right a messaging application, and I have two separate activities. I have the Connect activity, and the Client activity. I also have a class for the client. The client is used in both the Connect and Client activities, but I don't know how to carry the Client object used in the ConnectActivity to the ClientActivity.
My ConnectActivity:
public class ConnectActivity extends AppCompatActivity {
private Client client;
private EditText txtServerAddress;
private EditText txtPortNumber;
private EditText txtUsername;
private TextView lblErrors;
public ConnectActivity(){}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
final EditText txtServerAddress = (EditText) findViewById(R.id.edit_text_server_address);
this.txtServerAddress = txtServerAddress;
final EditText txtPortNumber = (EditText) findViewById(R.id.edit_text_port_number);
this.txtPortNumber = txtPortNumber;
final EditText txtUsername = (EditText) findViewById(R.id.edit_text_username);
this.txtUsername = txtUsername;
final TextView lblErrors = (TextView) findViewById(R.id.text_view_errors);
this.lblErrors = lblErrors;
Button btnLogIn = (Button) findViewById(R.id.button_log_in);
btnLogIn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String username = txtUsername.getText().toString().trim();
if(username.length() == 0)
return;
String portNumber = txtPortNumber.getText().toString().trim();
if(portNumber.length() == 0)
return;
String serverAddress = txtServerAddress.getText().toString().trim();
if(serverAddress.length() == 0)
return;
int port;
try
{
port = Integer.parseInt(portNumber);
}
catch(Exception en)
{
return;
}
client = new Client(serverAddress, port, username, new ClientActivity(), ConnectActivity.this);
if(!client.start())
return;
Intent intent = new Intent(ConnectActivity.this, ClientActivity.class);
startActivity(intent);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void append(String str)
{
lblErrors.setText(str);
}
}
My ClientActivity:
public class ClientActivity extends AppCompatActivity {
// for I/O
private Client client;
private ListView lstMessages;
private EditText txtMessage;
private Button btnSend;
private ArrayList<ChatMessage> messages;
private ArrayAdapter<ChatMessage> messageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
final ListView lstMessages = (ListView) findViewById(R.id.list_messages);
final EditText txtMessage = (EditText) findViewById(R.id.edit_text_message);
final Button btnSend = (Button) findViewById(R.id.button_send);
btnSend.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
txtMessage.setText("");
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, message));
}
});
messages = new ArrayList<>();
messageAdapter = new ArrayAdapter<ChatMessage>(ClientActivity.this, R.layout.chat_message, messages);
lstMessages.setAdapter(messageAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void connectionFailed()
{
Intent intent = new Intent(this, ConnectActivity.class);
startActivity(intent);
}
public void display(String msg)
{
messageAdapter.add(new ChatMessage(ChatMessage.ERROR, msg));
}
public void append(String str)
{
messageAdapter.add(new ChatMessage(ChatMessage.MESSAGE, str));
}
}
You can use Parcelable to pass an object from one Activity to another.
From your ConnectActivity.java btnLogIn onClick method , parcel Client object:
// Bundle
Bundle bundle = new Bundle();
bundle.putParcelable("CLIENT", client);
// Intent
Intent intent = new Intent(ConnectActivity.this, ClientActivity.class);
intent.putExtras(bundle);
startActivity(intent);
In your ClientActivity.java onCreate() method, get Client object:
// Get data from Intent
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
Client client = bundle.getParcelable("CLIENT");
}
FYI, Make sure your Client class implement Parcelable.
Hope this will help~
You have several options for sharing data between activities:
If the data should be permanent, store it in a database, flat file, or SharedPreferences. Each activity accesses the data directly from the permanent storage.
If the data should not be stored permanently, make your Client class Parcelable and pass it with an Intent. Others have already described this, so I will not go into more details.
Make your Client class implement Parcelable then pass the Client object in the Intent using
Bundle bundle = new Bundle();
bundle.putParcelable("client", client);
intent.putExtra(bundle);
startActivity(intent);
Here is a sample code to implement Parcelable.
You can easly do it by create an instance of Class A in Class B and pass anything you want
In my code below I am able to edit a text from my first setText() call but not from within the callback.
The error I receive is a NullPointerException for title in the callback.
How do I set this up so I can edit the text from within the callback?
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
setContentView(R.layout.listing_activity);
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}
Your setContentView() method must be called before giving references using findViewById() method, so your code will be -
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing_activity);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}
title.setText("listingId");//fine
That shouldn't be fine...
You must put setContentView before any findViewById. That is number one reason why your TextView is null.
Your callback is fine.
public class MainActivity extends Activity
{
public static String EXTRA_MESSAGE;
private Intent ceec;
private EditText cc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
public void sendMessage(View view)
{
ceec = new Intent(this, ToActivity.class);
cc = (EditText) findViewById(R.id.edit_message);
String message = cc.getText().toString();
ceec.putExtra(EXTRA_MESSAGE, message);
startActivity(ceec);
}}
And
public class ToActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
textView.setTextColor(Color.rgb(5,8,100));
if( message == "hi"){
textView.setTextSize(80);
}
// Set the text view as the activity layout
setContentView(textView);
}}
[ if( message == "hi"){
textView.setTextSize(80);
} ]
It didn't work why? And how to fix it and thank you
Instead of
message == "hi"
You should do:
message.equal("hi")
Never compare Strings with ==, check out this question to understand why.
Use .equals() method as,
if(message.equals("hi")){
textView.setTextSize(80);
}
else
{
// do your else stuff
}
this should work.
I had fragmented my code so as to follow oop concept thus a code that appears in four activities was made into a separate activity of its own .. now this child activities actions depends on from which of the four parent activities it was called.. so the setContent view value varies according to the parent activity..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_view);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
I got it...
all you have to do is to call the intent in the on create function before the setcontent view method..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
String layout = intent.getStringExtra("layout");
int id =getResourceUsingName(layout,"layout");
setContentView(id);
}
private int getResourceUsingName(String name, String type) {
mContext=getApplicationContext();
Resources resources = mContext.getResources();
int resourceId = resources.getIdentifier(name, type,
mContext.getPackageName());
return resourceId;
}
I am trying to pull in a string from one activity to another but every time I try to open the one activity I get a forceclose on .getString.
Logcat Error
10-26 10:36:45.444: ERROR/AndroidRuntime(15112): at http.www.hotapp.com.timeandlocation.email.EmailSettings.onCreate(ThisActivity.java:29)
Activity Calling the string
public class ThisActivity extends Activity{
private TextView reciever;
private String rec;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.elayout);
reciever = (TextView) findViewById (R.id.Reciver);
rec =
getIntent()
.getExtras()
.getString
("send");
reciever.setText(rec);
}
Activity with the string
public class OtherActivity extends Activity{
private EditText sendto;
private Button save;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.emailactivity);
sendto = (EditText) findViewById(R.id.sendto);
save = (Button) findViewById(R.id.Save);
save.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (EmailActivity.this,EmailSettings.class);
intent.putExtra("send", sendto.getText().toString());
startActivity(intent);
}
});
}
}
Thanks for the help.
try using getIntent().getStringExtra("send");
If you want to check if the extra is there use:
getIntent().hasExtra("send");
Try this:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
rec = extras.getString("send");
}
try this by eliminating NULL EXCEPTION from getString() function