Illegal start of expression and expected ';' errors in Android code - android

I just wanted to create an application that works like an HTML page or something. Just after clicked button, new site is opening.
I don't know why there are 5 errors: 2 Errors:(15, 11 and 15,18) error: illegal start of expression, and 2 Errors:(15, 31 and 15,41) error: ';' expected.
Here's the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
{
public void nextStep(View view) {
Intent i = new Intent(this, SecondaryActivity.class);
startActivity(i);
}
}
}
Coud you help me? :/

your message is not formated
It shows like if
{ public void nextStep(View view) {Intent i = new Intent(this, SecondaryActivity.class);startActivity(i); }
is inside you onCreate function,
if true, you can't write a function inside another function

There was problem with bracket.Try this
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void nextStep(View view) {
Intent i = new Intent(this, SecondaryActivity.class);
startActivity(i);
}
}

Related

No Button Feedback In Android Studio

no button feedback in android studio, i just want to guide from one activity to another! :(
no errors
here's the code
public abstract class OrderActivity extends AppCompatActivity {
private Button clickButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
clickButton =(Button) findViewById(R.id.IDbutton2);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity();
}
});
}
public void openActivity(){
Intent intent = new Intent(OrderActivity.this, orderPage.class);
startActivity(intent);
}
}
using nexus 6 emulator
please help guys
You haven't set a content view, so your OrderActivity has no layout. As such, there is no view with ID IDbutton2. This causes clickButton to be null, and not do anything.
You can see a very similar example in the Android training (I've removed the irrelevant bits):
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
}

How to send array of objects to activity

I read some example on this website and other but still have error.
It's not a compilation error but my application crash when I click on the button.
There is code of my MainActivity.java (only interesting part) :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//blablabla
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myLayout= (LinearLayout) findViewById(R.id.layoutProp);
Button button = (Button) findViewById(R.id.buttonValider);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text); // this one does not work
intent.putExtra(EXTRA_NUMBER, nbTextView);
startActivity(intent);
}
}
And code in Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
int nbTextView = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0);
MyTextView[] text = (MyTextView[]) intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.TVtest);
textView1.setText(""+ nbTextView);
} }
if I comment : MyTextView[] text =(MyTextView[])intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
the application does not crash.
Thank you all very much for your help
You cannot pass views through intents. Views are part of the layout and cannot be shared between activities.
If you want to pass array with custom objects you must implement a Parcelable or Serializable class and use:
getIntent().getParcelableExtra("array");
or
getIntent().getSerializableExtra("array");
Parcelable class will be faster but it is harder to implement.
Another way is to use a library like Gson to parse your data to Json and pass it as a string.

Can setContentView be replaced by android-annotations when espresso is used?

I have espresso test which verifies that text is displayed:
public class InformationActivityTests {
#Rule
public ActivityTestRule<InformationActivity_> mInformationActivityTestRule =
new ActivityTestRule<InformationActivity_>(InformationActivity_.class) {
#Override
protected Intent getActivityIntent() {
Intent i = new Intent(getTargetContext(), InformationActivity_.class);
i.putExtra("INFORMATION", "Espresso");
return i;
}
};
#Test
public void textIsDisplayed() {
onView(withText("Espresso")).check(matches(isDisplayed()));
}
}
This test passes when Activity has following code:
#EActivity
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
but fails when I "move" setContentView to #EActivity annotation:
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Error is:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edu/com.edu.InformationActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Am I doing something wrong or is it an issue with espresso / android-annotations?
I've checked code generated by android-annotations when using #EActivity(R.layout.activity_information) and this is how onCreate looks like in generated class (InformationActivity_):
public void onCreate(Bundle savedInstanceState) {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
init_(savedInstanceState);
super.onCreate(savedInstanceState);
OnViewChangedNotifier.replaceNotifier(previousNotifier);
setContentView(R.layout.activity_information);
}
the problem is that it first calls super.onCreate (so onCreate from InformationActivity) where I handle intent and TextView and then it calls setContentView and this can't work like that.
The solution for this is what Be_Negative suggested, so using #AfterViews annotation. It also simplifies code a bit (I could remove onCreate method):
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#AfterViews
void handleIntent() {
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Your problem is that you are never initializing informationview
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView; //<-- Null
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information); //<--STILL NULL
}
}
So you first will have to initialize that view.

passing data from activity to activity in android

What is the problem with my code :(
This is the sending class:
public class Send extends AppCompatActivity {
String message_text;
final static String MSG_KEY = "this.is.the.message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_layout);
}
public void sendMessage(View view) {
EditText entryText = (EditText)findViewById(R.id.message_text);
message_text = entryText.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(message_text, MSG_KEY);
startActivity(intent);
}
}
this is the receiver:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView theMessage = (TextView)findViewById(R.id.theMessage);
theMessage.setText(getIntent().getStringExtra("this.is.the.message"));
}
}
the app starts but it does not pass text, just an empty recieving activity ???
intent.putExtra(message_text, MSG_KEY);
replace with
intent.putExtra(MSG_KEY, message_text);
Frist argument is NAME , second argument - VALUE
You are using value as key and key as value which is causing this issue.
Change
intent.putExtra(message_text, MSG_KEY);
to
intent.putExtra(MSG_KEY, message_text);
If changing intent.putExtra(MSG_KEY, message_text); doesn't fix the issue. Check AndroidManifest.xml to make sure that MainActivity is not the launcher otherwise your MainActivity starts before the send class.

How can i access or execute .java file in another javafile in android app development?

i got 2 java files app.java and gallaery.java
App.java
public class App extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Listvideo = (Button) findViewById(R.id.Listvideo);
Listvideo.setOnClickListener(this);
public void onClick(View view) {
if (view == findViewById(R.id.Listvideo)) {
// i have to call gallery.java here which executes list of images in the android pone
}
}
Assuming Gallery.java also extends Activity you should call:
Intent mIntent = new Intent(this, Gallery.class);
startActivity(mIntent);

Categories

Resources