Activity won't open - android

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!

Related

Android Studio button onClickListener is not working to switch Layout

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

app unfortunately stopped when use "Intent intent = new ...." method to change screen

I have created a simple app.
one screen of my app has following code and I use a Button (id "sendManual") to change the app view to another one.
please look at the end of this code, you can seen the button id "sendManual".
I have setup it to change screen view to App2Activity.java
(I have put the Code of App2Activity java below this code, then you can check it also)
I installed this app on my android phone, But when I click Button(id"sendManual") app suddenly Stopped and says
Unfortunately,XXXXX app has stopped
public class showPermission implements View.OnClickListener {
Dialog dialog;
Activity activity;
public void showDialog(final Activity activity){
dialog = new Dialog(activity);
dialog.setCancelable(false);
this.activity=activity;
dialog.setTitle("XXXXX");
dialog.setContentView(R.layout.permission);
Button Sendcancel = (Button) dialog.findViewById(R.id.sendCancel);
Button SendNow = (Button) dialog.findViewById(R.id.sendNow);
Button sendManual = (Button) dialog.findViewById(R.id.sendManual);
sendManual.setOnClickListener(this);
SendNow.setOnClickListener(this);
Sendcancel.setOnClickListener(this);
dialog.show();
}
#Override
public void onClick(View view) {
int id=view.getId();
if(id==R.id.sendCancel){
dialog.dismiss();
}
else if(id==R.id.sendNow){
sendSMS();
}
else if(id==R.id.sendManual){
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
}
}
Here is the code of App2Activity.java
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMS2();
}
});
}
............ etc
Here is manifest file.
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<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>
</application>
You are not properly set context in Intent.Use this
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
instead of
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
This all is happening because you didn't declare App2Activity in Manifest.xml. That is compulsory to declare all the activities in manifest file.
Replace your manifest.xml with this one:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<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:name=".App2Activity"/>
</application>

Android Intent Declaration troubles

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

Unfortunately the app has stopped. What can I do to fix this?

I am programming an app. There is no errors. I can run the app, but when I press the button it says:
Unfortunately the app has stopped.
What can I do?
Here is my activity code:
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 =(Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
private void button1Click()
{
startActivity (new Intent ("com.example.cp3.tutorial.Class2"));
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
button1Click();
}
}
and the manifest:
<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.cp3.tutorial.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.cp3.tutorial.Class2"
android:label="#string/title_activity_class2" >
</activity>
</application>
</manifest>
Change button1Click() like this:
private void button1Click()
{
startActivity (new Intent (MainActivity.this, Class2.class));
}
startActivity (new Intent ("com.example.cp3.tutorial.Class2"));
here in your code! you refer to a package to be opend. if you want to open an activity on button click use this code:
Intent intent = new Intent(firstActivity.this, secoundActivity.class);
startActivity(intent);
put this on your click listener, instead of firstActivity put your current activity and secoundActivity put your activity name you want to be opened.
Hope this help you.

one activity to another one

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

Categories

Resources