I have created project in Android Studio with Navigation Drawer Activity and I have successfully created it,On navigation drawer activity I have created 3 section, In 1st section I put Button and I want to setOnClickListener method on that button which lead to start new Activity (ex. "xyz.class") I have used code
startActivity(new Intent(this,xyz.class));
but "this" keyword is not working and gives me error.
So, I changed code like
Context c;
startActivity(new Intent(c,xyz.class));
which gives NullPointerException,
My Section1 code is
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.pratik.crm50.R;
import com.gc.materialdesign.views.ButtonFloat;
public class Dashboard extends Fragment implements View.OnClickListener {
View rootView;
Context c;
private ButtonFloat float_btn;
private Button but;
private Button btn;
Context c;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
v = inflater.inflate(R.layout.dashboard_layout,container, false);
} else {
v = super.onCreateView(inflater,container, savedInstanceState);
}
View vv=v.findViewById(R.id.button_simple);
vv.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
// Toast.makeText(c,"Float Button",Toast.LENGTH_SHORT).show();
Log.e("ssas","sasafa");
//startActivity(new Intent(c,xyz.class));
}
}
and I can get successfully log Log.e("ssas","sasafa") on above code.
So how to do this?
Fragment does not extend Context while the Intent constructor expects the first parameter to be exactly that.
The Context c variable is redundant because a Fragment saves the parent activity by default.
You need to use the activity (class that extends Context) that the fragment is attached to:
startActivity(new Intent(getActivity(), xyz.class));
You get a NullPointerException, because you´re never assining a value to Context c.
So as user3249477 also mentioned use the method getActivity() to assign a value to c like c=getActivity();
Or call it directly while starting the Intent. This would look like this: startActivity(new Intent(getActivity(),xyz.class));
Related
I have here a problem with the onClicklistener method in a fragment. If I click on the button nothing happens and I don't know why. No error and no output.
I've tried to implements the OnClickListener or set a (Button) before the view.findViewById(R.id.button); but nothing helps.
I've seen much questions here in Stack Overflow about this problem but no solution from there helps me :/
Do you have any ideas? Thank You!
package com.christoph.myapplication.ui.home;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.christoph.myapplication.R;
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
final Button button= view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setBackgroundColor(Color.RED);
System.out.println("Hallo");
}
});
return view;
}
}
You can add click listener rather in onViewCreated function. Try it and let me know if it works. Check android fragment lifecycles documentation for more info.
Cheers
I just started to work in android studio. Tutorials are helping and all, but I can't find any help for this problem (it might be obvious, but I don't see it).
My problem comes after implementing the communication between the fragment activity and main activity.
Error after compiling is:
error: incompatible types: FragmentActivity cannot be converted to MainActivity
After going to where the error is, it says:
Incompatible types.
Required: com.app.appname.mainactivity
Found: androidx.fragment.app.FragmentActivity
This is the code for my fragment, where the error happens:
(names are in my language)
package com.ors.herobomb;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Uspeh extends Fragment {
EditText upis;
Button poslji;
public Uspeh() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View p = inflater.inflate(R.layout.fragment_uspeh, container, false);
upis = p.findViewById(R.id.hero);
poslji = p.findViewById(R.id.poslji);
poslji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ode = upis.getText().toString();
if(ode.length()>1){
MainActivity main = getActivity();
main.send(ode);
}
}
});
return p;
}
}
Answer provided by Daniel.
Replacing:
MainActivity main = getActivity();
With:
MainActivity main = (MainActivity) getActivity();
Solved my problem.
I am trying to create edittext using Java in bottom fragment class based on input that is to be passed from top fragment. But when I type
Button add_submit = new Button(this);
I get error for the this for parameter. However I can use this code in MainActivity.java.
Why is this so? What is causing the error and how to fix it?
The following are the complete code for the class
package com.test.gpacalc;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class AddActivityBottom extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_bottom,container,false);
return view;
}
public void createAddInput(String number_of_subjects){
Button add_submit = new Button(this);
}
}
Button constructor requires context as an argument. Fragment doesn't implement it, activity does. try new Button(getActivity())
I've created Activity and added a fragment to it using FragmentManeger. When i use android.app.Fragment and press the back button my application closes. When i use android.support.v4.app.Fragment and press the back button, the fragment is removed from the activity, but application is still working. I can't really understand why is that happening.
Here is the code i used:
Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.content_fragment, new Fragment1())
.addToBackStack("first")
.commit();
}
}
Fragment:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
When i simply replace import in Activity and Fragment to same classes, but in the support library, the result is different...
EDIT:
I also replaced getFragemetManeger() to getSupportFragmentMeneger() and it still works different
if you're only looking to change the back behavior, you can try overriding
onBackPressed with something like:
if(backstackCount() > 0)
{
super.onBackPressed
}
The issue occured because i used android.support.v7.app.AppCompatActivity and android.app.Fragment. I should've used android.app.Fragment with android.app.Activity or AppCompatActivity with support fragment.
I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.