android intents - android

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?

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 app keeps crashing, trying to move from one activity to another

**** 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));
}
});

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