Reputation: 177
my mainactivity.java looks now like this :
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FirstTabFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.main, null);
final View button = v.findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onlogin(v);
}
});
return v;
}
public void onlogin(View button) {
final Context context = getActivity().getApplicationContext();
final Intent intent = new Intent(context, user.class);
context.startActivity(intent);
}
}
and my application is still crushing when i click on the button :S
LogCat :
04-30 20:18:50.520: E/AndroidRuntime(17203): FATAL EXCEPTION: main 04-30 20:18:50.520: E/AndroidRuntime(17203): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.app.ContextImpl.startActivity(ContextImpl.java:1095) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276) 04-30 20:18:50.520: E/AndroidRuntime(17203): at com.example.bloodneeded.FirstTabFragment.onlogin(FirstTabFragment.java:37) 04-30 20:18:50.520: E/AndroidRuntime(17203): at com.example.bloodneeded.FirstTabFragment$1.onClick(FirstTabFragment.java:28) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.view.View.performClick(View.java:3549) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.view.View$PerformClick.run(View.java:14400) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.os.Handler.handleCallback(Handler.java:605) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.os.Handler.dispatchMessage(Handler.java:92) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.os.Looper.loop(Looper.java:154) 04-30 20:18:50.520: E/AndroidRuntime(17203): at android.app.ActivityThread.main(ActivityThread.java:4944) 04-30 20:18:50.520: E/AndroidRuntime(17203): at java.lang.reflect.Method.invokeNative(Native Method) 04-30 20:18:50.520: E/AndroidRuntime(17203): at java.lang.reflect.Method.invoke(Method.java:511) 04-30 20:18:50.520: E/AndroidRuntime(17203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-30 20:18:50.520: E/AndroidRuntime(17203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30 20:18:50.520: E/AndroidRuntime(17203): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 782
Reputation: 17105
I think you don't get the point what "this" really means. The setClass takes two paremeters - the Context and the Class. If you do this in an Activity, you specify "this" because the Activity extends Context (another words, and Activity is a Context). The Fragment is not a Context so you get this error. The Context can also be obtained in a different way. The startActivity method is also a Context method. And also it is less code to set the ComponentName from the constructor if an intent.
public void onlogin(View button) {
final Context context = getActvity();
final Intent intent = new Intent(context, user.class);
context.startActivity(intent);
}
Don't name classes with lower case letters. The class names should start with upper case letter. It's not an error, but rather a convention.
And about the getView method
return (RelativeLayout) inflater.inflate(R.layout.main, container,false);
There is absolutely no need to cast this to a Relativelayout. Just return the
return inflater.inflate(R.layout.main, container,false);
About
if (container == null) {
return null;
}
Not sure what it means, but mostly
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, null);
}
must be fine
Also it crashes because
public void onlogin(View button) {
final Context context = getActvity();
final Intent intent = new Intent(context, user.class);
context.startActivity(intent);
}
Must be defined in the context to which this View is attached.
This means that in order to work it must be defined in the Activity, not in Fragment. You have two ways of solving this 1) put this method in your Activity
2) If you want to define it in the Fragment, you should remove
android:onclick
from xml, set the button id
android:id="@+id/yourButtonId"
and set the onClickListener directly on the button.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.main, null);
final View button = v.findViewById(R.id.yourButtonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onlogin(v);
}
});
return v;
}
Upvotes: 1
Reputation: 20155
As you are in the Fragment, you should getActivity()
instead of this
Try this
intent.setClass(getActivity(), user.class);
Edit: also include this
getActivity().startActivity(intent);
Upvotes: 2