This is my social fragment activity and it contains buttons view. when i click on it i want to switch from one activity to other but when i lauch it on phone and click on button it says "unfortunately app stopped" what should i do to switch from one fragment to other fragment.
here is my social frag code:
SocialFrag
and i want to switch from social fragment to TechGadget Fragment on button click.
Here is Log cat
Log Cat
my App contains navigation drawer with tabhlayout and i declared buttons in one tab...while i want to open new activity when button gets clicked.
**#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.social_layout,container,false);
b = (Button) rootView.findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity().getApplicationContext(),TechGadgets.class);
startActivity(i);
}
});
return rootView;
}
}**
it reutrns error and here is log cat
04-28 01:07:07.300 17089-17089/com.example.android.techfnss E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.techfnss, PID: 17089
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.android.techfnss/com.example.android.techfnss.TechGadgets}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1885)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1579)
at android.app.Activity.startActivityForResult(Activity.java:3934)
at android.app.Activity.startActivityForResult(Activity.java:3894)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
at android.support.v4.app.Fragment.startActivity(Fragment.java:916)
at com.example.android.techfnss.SocialFragment$1.onClick(SocialFragment.java:26)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 01:07:10.275 17089-17089/com.example.android.techfnss I/Process: Sending signal. PID: 17089 SIG: 9
This question has been asked tens of times (at least), so you could've found an answer by a Google search. However, here is how you do it...
Button button = (Button) findViewById(R.id.my_button);
button.setOnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
};
Of course you would replace the relevant values with corresponding ones in your own code.
If you're looking for a more specific answer, ask a more specific question (with evidence that you have done research to find the answer yourself, or even at least a log trace from the crash you are getting).
You need to add setOnClickListener on your button inside onViewCreated method, like this --
public void onViewCreated(View view, Bundle savedInstanceState){
b = (Button)view.findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), 2nd_Activity.class);
startActivity(intent);
}
});
}
Hope this helps!
use this code -
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
here, OtherActivity.class is the name of activity that you want to go,and Mainactivity.this is your current activity.
You can pass the context to the Fragment when you create it and then reference it there. Or you can just use getActivity() instead of getActivity().getApplicationContext() but the first option is the best because sometimes getActivity() just doesn't work.
Always pass the context to the fragment.
Clearly it says that the activity is not declared in the manifest, instead of extend fragment, extend to any type of activity and add this line to your manifest inside the <aplication> </aplication> body:
<activity android:name=".TechGadgets"></activity>
Example:
<aplication>
...
<activity android:name=".TechGadgets"></activity>
...
</application>
And close this ask please
try using the below code
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
and also check did you added activity in manifest
<activity android:name=".TechGadgets"></activity>
Related
The code below is an onClick() method for a Button.
It keeps giving me an error.
If I delete the startActivity(), then it works fine.
How can I use a POST method when I click the Button, and also move to another Activity?
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
if (!validate())
Toast.makeText(getBaseContext(), "Enter user information!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute(herokuServer);
startActivity(new Intent(this, FoodPreference.class));
break;
}
startActivity(new Intent(Register1Activity.this, FoodPreference.class));
}
THE ERROR:
04-27 19:41:26.066 1974-1974/com.opshun_test.opshun_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.opshun_test.opshun_test, PID: 1974
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.opshun_test.opshun_test/com.opshun_test.opshun_test.FoodPreference}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.opshun_test.opshun_test.Register1Activity.onClick(Register1Activity.java:173)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-27 19:41:26.106 1974-2307/com.opshun_test.opshun_test I/output﹕ org.apache.http.client.methods.HttpPost#5b541ce
If you are using anonymous inner class for the click listener , use className.this instead of this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Activity2.class));
}
});
if you are handling the click in the activity, make sure the Activity is defined in the manifest file. Provide the stack trace for a complete solution.
You didn't declare you activity in you manifest. Add the following lines to you manifest file.
<activity
android:name=".FoodPreference"
android:label="#string/title_activity_food_preference" >
</activity>
Moreover, the more safe way to start an intent is always checking the intent is resolvable or not, like the following code.
Intent intent = new Intent(this, FoodPreference.class);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
// TODO: activity not found
}
Register your Activity FoodPreference in Manifest
and you are calling
new HttpAsyncTask().execute(herokuServer);
So better Intent to FoodPreference.class in onPostExecute()
My task: after button pressed, - second activity opening.
Problem: after button pressed, - "application closed unexpectedly".
LogCat said (short version):
04-10 21:25:24.968: E/AndroidRuntime(13032):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{cat.dog.szosta/cat.dog.szosta.ListaOcenActivity}:
java.lang.NullPointerException
LogCat said (full version):
https://drive.google.com/file/d/0B1jfkoUAwYVhYmFvSzBmS2ZIaU0/edit?usp=sharing
First activity code (partial):
private Button mOcenyPrzycisk;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mOcenyPrzycisk = (Button)findViewById(R.id.ocenyPrzycisk);
mOcenyPrzycisk.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intencja = new Intent(MainActivity.this, ListaOcenActivity.class);
startActivity(intencja);
}
}
);
}
Second activity(partial):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_ocen);
mprzyciskWroc = (Button) findViewById(R.id.przyciskWroc);
/*line 28*/ mprzyciskWroc.setOnClickListener(
new View.OnClickListener()
{
public void onClick (View v)
{
finish();
}
}
);
}
P.S: second activity declared in AndroidManifest.xml
I was looking in (didn't help):
Using Intent in an Android application to show another activity
android intents
Thanks in advance!
There is an exception in your second activity. (in ListaOcenActivity)
NullpointerException is thrown at line 28 of your ListaOcenActivity class, in the onCreate method. Search the method generating nullpointer exception, and handle it.
The way you are starting the activity is fine.
04-10 21:25:24.968: E/AndroidRuntime(13032): Caused by: java.lang.NullPointerException
04-10 21:25:24.968: E/AndroidRuntime(13032): at
cat.dog.szosta.ListaOcenActivity.onCreate(ListaOcenActivity.java:28) //here you can see its in your 2nd activity
Problem is here: cat.dog.szosta.ListaOcenActivity.onCreate(ListaOcenActivity.java:28)
Looking at your LogCat (long version) it shows that the NPE is in class ListaOceanActivity line 28. Which means this code is correct. Look into that class instead.
I have a really beginner question about activities, I have just started a new project and I added a button. I would like to know how I can create a second activity (in eclipse) and THEN how do I link the first activity to the second with a button.
To open another activity from an activity you should use Intents.
Tutorial from android doc: http://developer.android.com/guide/components/intents-filters.html
Example:
// The context, The activity to open
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
Intent constructor, startActivity
It will open NewActivity activity, you should replace NewActivity.class with the Class name to open.
And remember you should add the Activity in the AndroidManifest.xml
Since you asked, to open an Activity at button click you need to use the OnClickListener of the Button, setOnClickListener will be used to set the listener.
// i get the reference to the button from the XML
Button button = (Button)findViewById(R.id.button);
// now i set the listener
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Here you should add the code you want to execute when the button is clicked
// In our case we want to open the activity
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
// ... and stop.
}
});
new View.OnClickListener() this line creates an anonymous class which implements interface View.OnClickListener... Read more here if you want to know.
I have come across a strange error when i press a button that i supposed to return to teh main activity i get an activity not found error. But when i dod it from another activity, it works fine.
Working call:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Broken Call:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Campaign.this, Menu.class);
i.putExtra("Token", player.tokens);
// intent.putExtra("Round", player.round);
// intent.putExtra("Rank", player.rank);
// intent.putExtra("Score", player.score);
// intent.putExtra("Sec", player.secondsTapped);
// intent.putExtra("Min", player.minutesTapped);
// intent.putExtra("Hour", player.hoursTapped);
// intent.putExtra("Day", player.daysTapped);
// intent.putExtra("LifeTap", player.tapsInLife);
// intent.putExtra("SecTap", player.tapsPerSec);
// intent.putExtra("TapRo", player.tapps);
startActivity(i);
}
});
They are pretty much identical, but somehow the second one does not work. The first one has a bridging class before it. I looked through my xml file and nothing is wrong there since it works in one of my activities. The exact error i get is:
Unable to fin explicit activity class {com.tap.tapalot/android.view.Menu};
Thank You for Your help :)
I think you may have used wrong imports, so that Menu class is not the Menu-class you expect.
Try to specify the Menu.class in your intent with full package-path.
Check you seem to be importing. You are importing android.view.Menu in the second call. This is not your Activity class.
You are importing the wrong android.view.Menu class. You should import your Menu Activity class.
make sure you define 'Menu Class' in your mainfest file? like this:
<activity
android:name="pakgae.name.your.Menu"
android:label="#string/title_activity_menu" >
</activity>
I'm new to Android Dev, so please help me out.
I'm trying to start a new activity after i press a button but nothing seems to work.
Here's my code:
public class viewInfo extends Activity {
private Button btn;
public TextView txt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
btn=(Button)findViewById(R.id.buy);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent myIntent = new Intent(viewInfo.this, buyNow.class);
startActivity(myIntent);
}
});
}
I've also added this new activity in the Manifest but it keeps crushing after I press the button.
What am I doing wrong?
Misread the question initially (original answer below for completeness sake).
Make sure you have the activity you are calling defined in your manifest file:
Something like
<activity android:name=".buyNow" android:label="#string/app_name"></activity>
within the application tags would suffice.
Here's the original answer.
Assuming that you have the correct button ID - try this in your onclick:
Intent myIntent = new Intent(getApplicationContext(), buyNow.class);
startActivity(myIntent);
You could add a log message inside your onClick too, to make sure it is actually being called. You can see the log from logcat (run via adb logcat on the command line)
Try to make it:
startActivity(new Intent("[Here is your package name.what you declared in Manifest file]"));
For this you need to write to your manifest:
Hope it helps.
There may be a problem with your buyNow activity that is causing the error.
You really need to use logcat to trace the error. You can enable this by clicking the menu item:
Window -> Show View -> Other...
the selecting "LogCat" from the Android folder
You can simply do this:
startActivity(new Intent(getBaseContext(),Activity.class));
Afther you registred your activity in manifest:
<activity
android:name="com.example.ActivityName"
android:label="#string/app_name" >
</activity>