Shifting between activities (Android) - java

Shifting between activities isn't working.
What the app does is it opens the android.xml file then after 5 secs open the main file
Here's the code:
android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.android"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANDROID" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android.java file:
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class android extends Activity{
MediaPlayer sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
sound1 = MediaPlayer.create(android.this , R.raw.android);
sound1.start();
Thread timer = new Thread()
{public void run()
{
try{
sleep(5000);
}
catch(InterruptedException ac){
ac.printStackTrace();
}
finally{
Intent openactivity = new Intent ("android.intent.action.MAIN");
startActivity(openactivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sound1.release();
finish();
}
}
Mainactivity.java:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
android.xml file:(the first file that launches up)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background = "#drawable/android"
>
</LinearLayout>

Try this way
Intent openactivity = new Intent(android.this,MainActivity.class);
startActivity(openactivity);
And for more information go to starting-new-activity

First, always Capitalise 1st letter of class names (code convention),
and then write your intents like below:
Intent intent = new Intent(android.this, MainActivity.class);
startActivity(intent );
You can check Starting Another Activity in Android Developers website.

Related

Android BroadcastReceiver in ScrollActivity

I had working BroadcastReceiver which notifys user about network status changes, but since i added ScrollActivity to my app, the receiver is not working anymore. So i tried to make a blank ScrollActivity template project to test the reciever and i can see the same - the receiver wont work in apps with ScrollActivity. im a little bit desperate with this situation, would be thankful for help.
I have permissions and receiver registred in manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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=".ScrollingActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ConnectivityStatusReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Here comes the receiver code, which hasnt been called even once in scrollactivity apps yet..
package com.example.test_app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//THIS HASNT BEEN CALLED EVEN ONCE YET SINCE SCROLLACTIVITY
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
Log.d("RECIEVER_LOG", "intent:"+intent.getAction());
if(activeNetworkInfo.isConnected()){
Log.d("NETWORK_LOG", "connected.");
}else{
Log.d("NETWORK_LOG", "not connected.");
}
if (activeNetworkInfo == null){
//no connection activity opens on connection loss
context.startActivity(new Intent(context , NoInternetConnection.class));
}
}
}
And here is the Scrolling activity itself, untouched as Android Studio generated it
package com.example.test_app;
import android.os.Bundle;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
toolBarLayout.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Would be thankful for any help.

Android - Go to new screen on button click

Followed a tutorial but doesnt seem to work on emulation.
MainActivity.java:
package b.myfirstapp;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}
}
Activity2.java:
package b.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="b.myfirstapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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:label="#string/app_name"
android:name=".Activity2" >
</activity>
</application>
</manifest>
There are no errors in the code, its just that when i emulate it it doesnt work, maybe ive coded it incorrectly, not too sure, it looks fine to me.
I'm new to android dev so im confused. Help!
UPDATED: (For clearance purposes with comments properly inserted)
I think you are missing the call to addListenerOnButton in your MainActivity class, for example inside onCreate right after setContentView....
Also, you should make it private since it's only going to be called from within this class.
You have not called your addListenerOnButton method so you have not set onClick handler to your button.
Your onCreate should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton()
}

Bringing first activity on top of back stack

I have two applications with one activity in each of them. Application A and application B. Application A has its activity's launch type as SingleTask. Both Activities have buttons calling each other. What I want is that when I launch activity A from activity B's button which is in turn launched from activity A's button, and then when I press back button activity B should come to foreground. But instead it is going back to launcher. I have attached code for reference.
Application A MainActivity:-
package com.example.appa;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appb", "com.example.appb.MainActivity");
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Application A Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Application B MainActivity:
package com.example.appb;
import com.example.appb.R;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appa", "com.example.appa.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
});
}
Application B Manifest is unmodified default mainfest
As it can be seen I have already tried FLAG_ACTIVITY_REORDER_TO_FRONT but it did not help.
P.S :- I cannot change launch type of activity in application A
Thanks in advance
The singleTask launch mode is not compatible with that behavior. You can set a different launch mode each time you start an activity by setting flags, but a better approach might be to reevaluate the choice made for default launch mode.
Tasks and back stack article.

Cannot cast from Fragment to FragmentListView

While several people have asked questions about casting fragments to activities (and how that doesn't work), I couldn't find a fix that worked for me. I'm going through this tutorial, and have a main activity that contains two fragments. I'm probably missing something fairly obvious:
There error I get: Cannot cast from Fragment to FragmentListView (line18)
MainActivity:
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends Activity implements FragmentListView.OnSiteSelectedListener{
Fragmentwebview web;
FragmentListView list;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
list = (FragmentListView) manager.findFragmentById(R.id.fragment1);
list.setRefrence(this);
}
#Override
public void onSiteSelected(int i) {
// TODO Auto-generated method stub
web = (Fragmentwebview) manager.findFragmentById(R.id.fragment2);
// Check for landscape mode
if (web!= null && web.isVisible())
{
web.setNewPage(i);
}
else
{
Intent intent = new Intent(this , FragmentSupport.class);
intent.putExtra("index", i);
startActivity(intent);
}
}
}
Manifest:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.numeraid.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.numeraid.FragmentSupport"
android:label="#string/title_activity_fragment_support" >
</activity>
<activity
android:name="com.example.numeraid.FragmentListView"
android:label="#string/title_activity_fragment_list_view" >
</activity>
<activity
android:name="com.example.numeraid.Fragmentwebview"
android:label="#string/title_activity_fragmentwebview" >
</activity>
</application>
</manifest>
The first fragment that's causing the error
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:name="com.example.NumerAid.list"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
EDIT: Adding FragmentListView class
package com.example.numeraid;
import android.R.string;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentListView extends Fragment implements AdapterView.OnItemClickListener
{
ListView list;
String [] websites = {"Google","Facebook","Twitter","Xda-developer"};
OnSiteSelectedListener SiteListener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlistview, container, false);
list = (ListView) v.findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<string>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1));
list.setOnItemClickListener(this);
return v;
}
#Override
public void onItemClick(AdapterView adapterView, View view, int position, long l)
{
SiteListener.onSiteSelected(position);
}
// Container Activity must implement this interface
public interface OnSiteSelectedListener {
public void onSiteSelected(int i);
}
public void setRefrence(OnSiteSelectedListener siteListener)
{
this.SiteListener = siteListener;
}
}
Thanks to #corsair for this answer, originally posted as a comment:
My guess is that your FragmentListView class extends android.support.v4.app.Fragment
instead of android.app.Fragment. If you want to use the support version, then you should
use getSupportFragmentManager() instead of getFragmentManager(). Otherwise, you need to
extend android.app.Fragment everywhere as done in the tutorial you are following.

Activity not declared in Manifest (Apparently)

I can't for my life figure out why it keeps saying "unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest?". As far as I can tell it has been declared and i have followed the convention of the last small app I built. My xml files are lowercase, my .java files are CamelCase. Does anyone have any other ideas as to why it would give me this error? The error is in the readyToBeep function in MainActivity.java.
Main Activity.java
package com.example.timer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readyToBeep(View v)
{
Intent intent = new Intent(this, Beep.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Beep.java
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Beep extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
}
private Context Context;
Timer timer;
public Beep(){};
public Beep(Context Context) {
this.Context = Context;
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1500); //subsequent rate
}
class RemindTask extends TimerTask {
public void run() {
MediaPlayer mPlayer = MediaPlayer.create(Context, R.raw.beep);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
}
}
public void main(String args[]) {
new Beep();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.timer.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.timer.buzz" >
</activity>
</application>
</manifest>
TIA
unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest
There is no declaration of your Beep activity in your manifest. Add it :
<activity android:name="com.example.timer.Beep" >
</activity>

Categories

Resources