I'm really new to Android. I'm trying to pass the data to the text view of another class. But for some reason, it does not appear in the second class / page (the textview didnt change). I was able to do it when I was not using android annotation, but I was told that using annotations is a lot easier so I'm trying to convert it to that but for some reason it isn't working? I might be missing something.
Main 1
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.world)
public class MainActivity extends AppCompatActivity {
#ViewById(R.id.userName)
EditText user;
#Click(R.id.signIn)
public void signInButton(){
Main2Activity_.intent().name(user.getText().toString()).start();
}
Main 2
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.welcome)
public class Main2Activity extends AppCompatActivity {
#Extra("name")
String n;
#ViewById(R.id.name)
TextView tv;
Intent in2 = getIntent();
#AfterViews
void home(){
tv.setText("Hello, " + n + " !");
}
}
In your receiving Activity (for example MyActivity) you must declare the name of the object you want to receive like that
#Extra("name")
String name;
To pass data, when you create your intent you must do something like this:
Main2Activity_.intent(this).name(user.getText().toString()).start();
If you need a more precise solution edit your question to show more of your code.
You can look at the official doc for a more complete example
It might have to do with the dependencies you should add for the annotations to your project_root/build.gradle and app/build.gradle files.
Make sure to check this thread: what is Android Annotations and why we use Android Annotations?
Related
I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.
This should be very simple and I'm really not sure what is going wrong. I have created a SMSReceiver java class that basically receives messages and then gives them to my MainActivity. When the onReceive() function fires, the app crashes because it is having issues sending the data back to MainActivity. It has been throwing a NullPointerException on the Zout() call in onReceive().
Here is the SmsReceiver class:
package ...; //you don't need to see my dumb package name.
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
public class SmsReceiver extends BroadcastReceiver {
ArrayList<String> phoneNumbers;
ArrayList<String> textMessages;
private Activity mainAct; //our reference to MainActivity
//very simple constructor
public SmsReceiver() {
}
//this function is called in onCreate() in MainActivity to provide reference to MainActivity
public void SetSMSReceiverActivity(Activity a){
mainAct = a;
//talk to our main activity
MainActivity mainComm = (MainActivity)mainAct;
mainComm.Zout("SMSReceiver initialized bro"); //this works just fine
}
#Override
public void onReceive(Context context, Intent intent) {
//THIS IS WHAT CAUSES ISSUES! This call to MainActivity does NOT work for whatever reason. Rude.
MainActivity mainComm = (MainActivity)mainAct;
mainComm.Zout("This message won't send"); //this does NOT work fine for whatever reason. Throws NullPointerException.
//...more code down here that actually works!
}
}
The function Zout() in my MainActivity is a simple print to TextView for debugging purposes. I will be using a function in MainActivity to receive the data once onReceive() stops being moody.
I'm sorry if this is a dumb question. I haven't programmed in Java for a few years and I'm quite rusty. I have no idea why this isn't working. I'll be tossing out +1's to whoever can help!
I believe it is because you have not initialized your mainAct variable.
for onCreate(){}
you do initialize
mainAct = a;
but not for onReceive.
Method is undefined for the type in Eclipse. Can't seem to solve it. The error is in the lines: msg.setTimestamp( System.currentTimeMillis() ); and msg.setBody("this is a test SMS message");
package com.example.smsnotification;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
public class PopSMSActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Retrieve serializable sms message object by the key "msg" used to pass to it
Intent in = this.getIntent();
PopMessage msg = (PopMessage) in.getSerializableExtra("msg");
//Case where we launch the app to test the UI EG: No incoming SMS
if(msg==null){
msg = new PopMessage();
con.setPhone("0123456789");
msg.setTimestamp( System.currentTimeMillis() );
msg.setBody("this is a test SMS message");
}
showDialog(msg);
}
Remove the code and write it back to eclipse. It worked for me....You can try copy and paste to after writing signature of function/class.
This means the PopMessage class doesn't provide the methods setTimestamp(long) and setBody(String).
There is no import statement for PopMessage in your code so I assume it is a class which you have implemented and is contained in the same package as the Activity which you have posted.
So you could either solve this by implementing those two methods in PopMessage or by removing the calls.
You may also extend your Eclipse Settings by activating the "save Actions" (Window->Preferences->Java->Editor->Save Actions) and use the Option "Organize Imports". This would at least add the propbably missing Import "...PopMessage" while you press Ctrl+S.
I import an existing project into eclipse, and always encountered this problem. First, it appears the error: R cannot be solved. After import android.R. the error turned to the following methods. "start cannot be or is not a field"
I also checked the gen folder, where contains a R.java file. The file looks good, including the following contents: I also tried to clear the project and rebuilt which didnot work. Does any body have any idea how to solve this? Thanks a lot.
R.java:
public static final class layout {
public static final int kbd=0x7f030000;
public static final int main=0x7f030001;
public static final int start=0x7f030002;
}
startactivity.java:
import android.R;
import android.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class StartActivity extends Activity {
Intent mMain;
/** Called when the activity is first created. */
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);//where error appears**
}
public void onStartButtonClick(View v) {
mMain = new Intent(this, MainActivity.class);
startActivity(mMain);
}
you are importing
android.R
first remove android.R and android.R.layout from import
and import R with your packagename.R
for eg. com.example.R instead of android.R
Try doing Project -> Clean. If that doesn't work it looks like this might have already been answered: R cannot be resolved - Android error
To any one who may face the similar problem with me. I found an answer from the link below:
R cannot be resolved - Android error
My problem was solved by:
1. delete all the imports lines.
2. press ctrl+shift+O to manually regenerate all imports.
And then the problem disappeared.
Many thanks for all the comments above.
I'm so sick of android errors in eclipse.. this has been happening for too long but here it goes.
here's the code
import android.app.Activity;
import android.os.Bundle;
import android.R;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
//constants used when saving/restoring state
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
I just threw the imports in to see where the code starts
the first private static final (BILL_TOTAL) errors like this:
Multiple markers at this line
- Syntax error on token(s), misplaced
construct(s)
- Syntax error on tokens, delete these
tokens
- Syntax error on tokens, delete these
tokens
I delete the first private static final then the error jumps to the next static method declaration... ugh.. the code is 90% done and I know there are no errors.. (if anything a curly brace somewhere)..
Please help me understand these random eclipse errors.
thanks in advance
Also... in the midst of all this i lost access to my main layout
main cannot be resolved or is not a field
also one last thing
NONE of my variables "can be resolved as a variable"
Those private static final variables need to be inside of a class context:
import android.app.Activity;
import android.os.Bundle;
import android.R;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
class MyClass {
//constants used when saving/restoring state
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
...
}