I had created a simple message passing application in android studio to include this code into my Project work.But I was not able to switch the activity by passing a string into another activity.please help me out to find the problem.
When the button is triggered the application got ended itself instead of switching the activity.I had tried many ways to figure it out.
First Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
EditText text;
btn = (Button)findViewById(R.id.button);
text = (EditText)findViewById(R.id.editText);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String mess = text.getText().toString();
Intent i= new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("EXTRA",mess);
startActivity(i);
}
});
}
}
Second Activity(Main2Activity)
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
EditText editText = (EditText)findViewById(R.id.editText2);
editText.setText(getIntent().getStringExtra("EXTRA"));
}
}
The expected output was to exchange the data from one activity to another,
but now the application gets stopped.
Related
I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}
Android studio does not return me any errors but when I run this code emulator I receive a message Unfortunately, the app has stopped
I am connecting two activities in the android studio using OnClick and Intent. I tried to find errors through Logcat but even there, there are no any errors.
First page is std_home.java and second is std_registration.java
public class std_home extends AppCompatActivity {
private Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_std_home);
b2 = (Button) findViewById(R.id.std_sign_in);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
startActivity(new Intent(std_home.this, std_registration.class));
}
});
public class std_registration extends AppCompatActivity {
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_std_registration);
t = (TextView) findViewById(R.id.reg_title);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/AdobeMing-Light.ttf");
t.setTypeface(myCustomFont);
}
}
The expected result is after button Sign in is clicked app should open registration form but it stops the process with the message Unfortunately, the app has stopped.
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
I want to have my MainActivity which shows a list of different TextViews.
The second Activity contains an EditText field. When I hit 'Submit' in the second Activity, I want to add the String from EditText to the list of TextViews in MainActivity.
The problem I have is that there is a new MainActivity started each time, so that only the latest Text is shown. I want to go back to my MainActivity and kind of "collect" the texts in there. Here is some code:
The MainActivity:
public class FullTimeline extends AppCompatActivity {
private LinearLayout linLayout;
private FloatingActionButton addEntry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_timeline);
linLayout = (LinearLayout) findViewById(R.id.entriesLinear);
addEntry = (FloatingActionButton) findViewById(R.id.floatingActionButton); //starts the second Activity with the EditText
//Here I want to get the text from my second Activity
Intent intent = getIntent();
String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
linLayout.addView(createNewTextView(entry), 0);
}
public void openEntryActivity(View view){
Intent intent = new Intent(this, EntryTextActivity.class);
startActivity(intent);
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
return textView;
}
}
This is the second Activity with the EditText:
public class EntryTextActivity extends AppCompatActivity {
public static final String EXTRA_ENTRY = "com.xyz.ENTRY";
private Button submitButton;
private EditText editTextEntry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry_text);
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(EntryTextActivity.this, FullTimeline.class);
editTextEntry = (EditText) findViewById(R.id.editTextEntry);
String entryText = editTextEntry.getText().toString();
intent.putExtra(EXTRA_ENTRY, entryText);
startActivity(intent);//this starts a new instance of the MainActivity ?
}
});
}
}
I hope that helps understanding my problem. I could not find any working solutions on here.
You need to use startActivityForResult()
Here's a Nishant's simple explanation on how to work with it:
- https://stackoverflow.com/a/10407371/5648172
Basically, you want to start your EntryTextActivity with startActivityForResult() instead of startActivity().
I haven't tested it but i think that in the case you don't want to use a database, you can set your MainActivity as "singleInstance" in your manifest :
<activity android:name="{your }" android:launchMode= "singleInstance" />
Then you can add the new text in an your MainActivity's onResume
#Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
linLayout.addView(createNewTextView(entry), 0);
}
Probably something I do not understand.
In the application programs (from google play), if we move from the main activity to the second and then back to the main, then after pressing the "back" button on the phone, the application closes.
I tried to make my own applications with two activities, but it did not work as it should. When the main activity goes to the second and then I go back to the main, then after pressing the "back" button on the phone, the application instead of closing, it goes back to the second activity, then back to the main and it just closes.
What am I doing wrong ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Act2Butt = (Button) findViewById(R.id.Act2Butt);
Act2Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
.
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button Act1Butt = (Button) findViewById(R.id.Act1Butt);
Act1Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class );
startActivity(intent);
}
});
}
}
To go back to MainActivity from Main2Activity, you need to call onBackPressed() or finish(), instead of startActivity.