i am trying to switch from one activity to other using intents, first activity is running properly but when second activity starts the app gets stopped. i have attached the code with this.
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sampleapp"
android:versionCode="1"
android:versionName="1.0" >
...
<application
...
<activity
android:name="com.example.sampleapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.sampleapp.welwithname"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.WELWITHNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
...
</application>
</manifest>
this is the default activity. i tried to get the string and send it to the next activity.here is the xml and activity.
Activity Code:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.sampleapp.MESSAGE";
String tempname;
Button submit;
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit= (Button)findViewById(R.id.button1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Welwithname.class);
EditText editText = (EditText) findViewById(R.id.getname);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
Second Activity:
this activity gets the string from activity 1 and print the value..heres the xml and activity
Activity Code:
public class Welwithname extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welwithname);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText("Welcome" + message + "!!!" );
Button sq = (Button)findViewById(R.id.sq);
Button exit = (Button)findViewById(R.id.exit);
sq.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View V)
{
Intent main = new Intent(Welwithname.this,MainScreen.class);
startActivity(main);
}
});
// Set the text view as the activity layout
setContentView(textView);
}
...
}
what changes should i made in this code to make it run?
<activity
android:name="com.example.sampleapp.welwithname"
Isn't your class called Welwithname ?
capitalise the 'W' in the Activity name in your AndroidManifest.xml
You are not going to be a developer long if you do not read the console / logcat output when you come across a crash.
If you read your LogCat it is most likely saying "Cannot find Activity welwithname, have you declared it in your Manifest?" and that would be your answer and StackOverflow would be a happier place
http://developer.android.com/tools/help/logcat.html
Related
I have two buttons on my main Activity, which both should lead to another activity, but when I press my button, it doesn't change anything.
I have all three activities in my manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".UserActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".ModeratorActivity"
android:label="#string/app_name">
</activity>
Method for the two buttons in my main activity:
private void setUserTypeOnButtonClick(){
Button userButton = findViewById(R.id.button_user);
Button moderatorButton = findViewById(R.id.button_moderator);
userButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
System.out.println("Clicked userbutton");
Intent newActivity = new Intent(v.getContext(), UserActivity.class);
startActivity(newActivity);
}
});
moderatorButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
System.out.println("Clicked moderatorbutton");
Intent newActivity = new Intent(v.getContext(), ModeratorActivity.class);
startActivity(newActivity);
}
});
}
What is the problem here? Did I leave something out?
Edit : Solution is just to
call setUserTypeOnButtonClick() inside onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Created activity_main");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUserTypeOnButtonClick();
}
You forgot to call the setUserTypeOnButtonClick() method after setContentView() in onCreate().
The setContentView() does make the buttons visible but there is no click listener binding taking place, and hence the buttons don't do anything.
you just call the setUserTypeOnButtonClick() in your onCreate method
You just forgot call the setUserTypeOnButtonClick() method in onCreate()
You forget to call the method getUserTypeOnButtonClick().
ToggleButton cannot cast to Button.
And also ModeratorActivity and UserActivity OnBackPressed just call finish().
I need to close an activity when a button is clicked. Unfortunately, when button is clicked, the activity does disappear but is still in the background. User can still select it and it comes back to front. What I need is the activity completely gone/destroyed.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
I searched on SO on related questions, however, none of them help with closing the activity completely. I already tried adding return, adding another broadcast listener and passing command to call finish outside onCreate. So at this point, the question is - is this really possible or is this how Android works, you can call finish() but it is still in the background and user can re-launch it.
Here is xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app1.test.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT: Adding android:excludeFromRecents="true" does not solve the issue. Here are steps to recreate this, if anyone thinks it is a duplicate or already solved, please try and post comment/answer, I will accept it.
Open Android Studio
Create empty activity project.
Add a button.
Add code in MainActivity's onCreate for button click listener.
Inside click listener, call finish.
Run the app and click button and see if the activity is still in background or not.
Just give it a try.
In you manifest.
<activity
android:excludeFromRecents="true"
android:name=".Activities.SplashActivity"
android:theme="#style/AppThemeSubActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now in your java code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_search:
// Intent intent = new Intent(MainActivity.this, SearchActivity.class);
// startActivity(intent);
finish();
System.exit(0);}
}
put the extra line System.exit(0); after calling finish it works for me.
You need to put this in your XML manifest:android:excludeFromRecents="true"
in your Activity TAG.
<activity
...
android:excludeFromRecents="true">
</activity>
You could try this
android:excludeFromRecents="true", Use it in your manifest.
plz try this to go back
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
}
and this code to kill app process
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
When I launch my app from Eclipse to my phone, it launches the Launcher first and then my main activity. It successfully passes a variable to the main activity. I check that the username has been logged with a toast.
When I launch my app from my phone directly, it goes straight to the main activity; the main activity registers the variable as null.
I created a test app that performs EXACTLY the same function as the launcher in this one; their manifests are identical except for the activity names; and that test app functions correclty from the phone and when I install it from Eclipse.
this is a real brain-teaser.
Here is the code for the starter activity:
public class SecureAppStarter extends Activity {
TextView report;
WebView input;
Context thisContext = this;
String thisValue, url;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.starter);
initialize();
WebViewClient rclient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return false;
}
#Override
public void onLoadResource(WebView view, String url){
input.getSettings().setJavaScriptEnabled(true);
input.addJavascriptInterface(new CustomJavaScriptInterface(thisContext), "Android");
}
#Override
public void onPageFinished(WebView view, String url){
report.setText(""+thisValue);
if (thisValue!= null){
Intent passOn = new Intent("arbuckle.app.MainActivity");
passOn.putExtra("username", thisValue);
startActivity(passOn);
}
}
};
rclient.onPageFinished(input, url);
input.setWebViewClient(rclient);
input.loadUrl(url);
}
public class CustomJavaScriptInterface {
Context mContext;
CustomJavaScriptInterface(Context context) {
mContext = context;
}
public void getValue(String value){
thisValue = value;
}
}
private void initialize() {
report = (TextView) findViewById(R.id.tvViewName);
input = (WebView) findViewById(R.id.wbWebAuth);
url = "http://URL.of.data.com";
}
}
And here is the manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="SecureAppStarter"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ArbuckleAppActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="arbuckle.app.ArbuckleAppActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Well I figured it out.
I have been installing/uninstalling versions of the app for weeks where the main activity was the launcher. clearly that gets kept somewhere in Android cache and it was choosing the main activity to launch even though it wasn't the launcher anymore.
So I uninstalled the app completely; and reinstalled. Now it works.
Does anyone think I hacked at the problem instead of solving it? Should I expect further problems down the road?
My problem borns when i call
Intent i = new Intent(c.getApplicationContext(),ActivityMulti.class);
c.startActivity(i);
It generates the exception mentioned below:
10-02 17:54:26.037: ERROR/AndroidRuntime(905): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{package.com/package.ActivityMulti}:
java.lang.InstantiationException: package.ActivityMulti
This is called in an handler generate like this:
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
msg.setData(b);
handler.sendMessage(msg);
The second Activity is a simple one:
public class ActivityMulti extends Activity{
public ActivityMulti(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity multi");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
Why that exception is generated? Can you help me?
EDIT:
This is my manifest:
.
.
.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMulti"></activity>
</application>
</manifest>
EDIT 2:
I Solved! the problem was in constructor of the second Activity.. There is no need of it!
public class ActivitySingle extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity single");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
Have you added the activity you are starting in the Manifest file?
It's not important if you are starting it from Handler, since you have provided the right context for using .startActivity(). I think you have missed to add the activity in the manifest.
in my app I am trying to add new screen. In my activity I've:
public void addItem(View v) {
Intent i = new Intent(SQLiteListActivity.this, add_screen.class);
startActivity(i);
}
In add_screen.java:
public class add_screen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
In file add_screen.xml I've layout this screen. In file AndroidManifest.xml I added this next activity, especially:
<activity class=".Add_screen" android:name="ADD_ITEM" android:label="Add item">
</activity>
I am still getting an error message about "Application has been stopped." I am newbie in Android development, I tried to do this by some tutorial, I've everything by it, but I don't know why, I'm still getting the error message above...
Can you help me, please, with this problem? I've no idea, what could be wrong.
Try replacing the activity tag in your manifest with this instead:
<activity
android:name=".add_screen"
android:label="Add item"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You shouldn't need the class attribute, I don't even know if that's recognized.
Edit: Try the above instead?