android app keeps crashing, trying to move from one activity to another - android

**** I found the sulotion *****
all I add to do is to move all my function from "protected void onCreate(Bundle savedInstanceState)"
and put them in "public static class PlaceholderFragment extends Fragment implements View.OnClickListener". Thank you all.
I really new to all of this and I just can't manage to move from one activity to another...
I don't see why it doesn't work and keeps crashing every time. Can anyone help?
here's MainActivity code:
public class MainActivity extends ActionBarActivity {
Button button1;
#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();
}
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
}
private void buttonClick()
{
startActivity(new Intent("com.example.chaty.SignUpActivity"));
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
buttonClick();
break;
}
}
here is the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.chaty.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.chaty.SignUpActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.chaty.SignUpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
I am really new for all of this...

your app crashes, presumably for ClassCastException, because you are casting this to OnClickListener
button1.setOnClickListener((OnClickListener) this);
but your activity is not implementing the interface OnClickListener .

I would suggest you to change your button onClickListener like below.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(), SignUpActivity.class));
}
});

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

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.

Activity won't open

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!

android intents

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?

Categories

Resources