I want to make the main activity which have some buttons ,how can i jump from activity to another one . Why when i pressed the button he said : Unfortunately , mynameapp has stopped
This work properly :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlackPixel blackPixel;
blackPixel = new BlackPixel(this);
setContentView(blackPixel);
blackPixel.requestFocus();
});
}
Why this don't work ?:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// BlackPixel blackPixel;
// blackPixel = new BlackPixel(this);
// setContentView(blackPixel);
// blackPixel.requestFocus();
Button buttonSave=(Button)findViewById(R.id.buttonDraw);
buttonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,ButtonDraw.class));
}
});
}
Here is the buttonDraw class :
public class ButtonDraw extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
BlackPixel blackPixel;
blackPixel = new BlackPixel(this);
setContentView(blackPixel);
blackPixel.requestFocus();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:launchMode="singleInstance"
>
<activity
android:name="com.example.myfirstapp.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.buttons.ButtonDraw"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.buttonDraw" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
1- yes you can go to another Activity by
startActivity(new Intent(MainActivity.this,ButtonDraw.class));
2- Yes you can close your Activity with following code after above code:
this.finish();
for the learning about activity use this link and this
You can write the following line in of Androidmanifest.xml:-
android:launchMode="singleInstance"
then you don't need to write finish() every time
Related
I'm trying to change the layout when I clicked on a button but it didn't not work
my button is in activity_main.xml
and I want to switch to new_post.xml
here is my code in Activity main class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
this.getSupportActionBar().hide();
} catch (Exception e) {
}
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.postButton);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this , new_post.class);
startActivity(i);
}
});
}
my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finalprofile">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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=".show_more"></activity>
<activity android:name=".new_post" />
<activity android:name=".edit" />
<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>
what is the problem?
If you just want to change the layout on button click. There is no need to launch Activity again. Just set setContentView(R.layout.new_post) inside onClickListener.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.new_post)
}
});
But you have to take only one precaution that you will take care of any clicks or work on views that were in activity_main.xml and are not in new_post.xml
Happy Coding
I am creating android application for sending notification. I have create two Android application. Here I want to send some forms details via notification from first Android application to 2nd android application in Android.
And after that receiving the notification, click on the notification I want to open my second Android application. What should be done to send the push notification to the particular individual user in Android?
Here is my Activity one codes
public class Activity_One extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_one);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.customer.Activity_One","com.vendor.Activity_B"));
startActivity(intent);
}
});
}
}
Here is AndroidManifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Activity_One"
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>
Here is Activity two code
public class Activity_B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_two);
Button bn = (Button)findViewById(R.id.button1Send);
bn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.customer.Activity_One");
startActivity(intent);
}
});
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vendor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Activity_B"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action
android:name="com.vendor.Activity_B" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You do not need to use notifications at all. You can just use intents. That is the standard way to accomplish what you want to achieve. See Interacting with Other Apps for details.
From first application you can start a normal notification and from second application initialize a broadcast receiver and read notification
I am trying go from one activity to its sub-activity (i.e, a new page with more buttons), but every time I click the button, the application "Unfortunately stops running".
I believe that the flaw lies in the manifest where I might writing something wrong under the intent-filter section.
Mind taking a look?
public class MainActivity extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnButton2();
}
//First Activity
private void addListenerOnButton() {
// TODO Auto-generated method stub
button = (Button) findViewById(R.id.activity_one);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent IntentOne =
new Intent(arg0.getContext(), ActivityOne.class);
arg0.getContext().startActivity(IntentOne);
}
});
}
//Second Activity, will look into it later. Making it Explicit for now.
public void addListenerOnButton2() {
button = (Button)findViewById(R.id.activity_two);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View argo) {
Intent IntentTwo = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.ca"));
startActivity(IntentTwo);
}
});
}
}
///////////////////////////////////////////////////////
Here is the Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.poop"
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.poop.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.poop.ActivityOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.poop.ActivityOne" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I have not added ActivityTwo yet to the manifest.
You can't have Activities that aren't registered in the Manifest. I don't see an ActivityOne registered in your AndroidManifest.xml
I have a button that opens a new activity in Android, but it does nothing.
Java for first activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lists);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
ListView listsList = (ListView) findViewById(R.id.lists);
Button newList = (Button) findViewById(R.id.newlistbutton);
newList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), NewWishList.class);
startActivity(myIntent);
}
});
}
}
Java for second activity:
public class NewWishList extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlist);
Button back = (Button) findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ListOfLists.class);
startActivity(intent);
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
RadioGroup option = (RadioGroup) findViewById(R.id.radioGroup1);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wish.list"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-feature android:name="android.hardware.screen.portrait"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar">
<activity
android:name="com.wish.list.FacebookSignIn"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListOfLists"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
></activity>
<activity
android:name=".NewWishList"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
></activity>
</application>
</manifest>
No errors in Logcat or Error Log. It's very weird. The reason I have the onConfigurationChange is because I have it set to Force Portrait Orientation on. The activities are defined in the Manifest.
First, try using
MyActivity.this
for the context.
Instead of:
v.getcontext()
try
getApplicationContext()
What's the IDE you're using? Probably Android Studio?
Do this.
Exit studio.
delete .idea\workspace.xml file
Relaunch and try again
Worked for me on Android Studio 1.0!
I am working on android. I tried the following code but the application is not working and showing error dialog as application closed unexpectedly. It is showing error message as runtime error caused by java.lang.NullPointer exception.
I am including my code and manifest files here..
IntentsActivity.java
public class IntentsActivity extends Activity {
int request_code=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivityForResult(new Intent(IntentsActivity.this,AnotherActivity.class),request_code);
}
});
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
if(requestcode==request_code)
if(resultcode==RESULT_OK)
Toast.makeText(getBaseContext(), "Data returned is "+data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
AnotherActivity.java
public class AnotherActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.anotherxml);
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText edittext=(EditText)findViewById(R.id.editText1);
Intent i=new Intent();
i.setData(Uri.parse(edittext.getText().toString()));
setResult(RESULT_OK,i);
finish();
}
});
}
}
Manifestfile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.intent"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".IntentsActivity"
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=".AnotherActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.AnotherActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here main.xml file consists a button and another.xml consists consists a EditText and a button. Can anyone give me the reason why the application is not working
Cannot seem to find anything wrong with your code, my guess:
EditText edittext=(EditText)findViewById(R.id.editText1);
returns null?