I am getting this error. This is my logcat
. I don't know what exactly is wrong. I get revoke error or we can say NullExceptionError
. I tried everything but nothing workout.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
at com.nazeefawann.truemate.Profile.AccountSettingActivity.setViewPager(AccountSettingActivity.java:52)
at com.nazeefawann.truemate.Profile.AccountSettingActivity$2.onItemClick(AccountSettingActivity.java:71)
at android.widget.AdapterView.performItemClick(AdapterView.java:303)
at android.widget.AbsListView.performItemClick(AbsListView.java:1151)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3065)
at android.widget.AbsListView$3.run(AbsListView.java:3878)
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:5538)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
I am making an app and I want to make fragment in the settings activity instead of separate so that I can add more feature easily. I have created the 2 fragment but when I click on it return back to the previous activity. I don't know why
public void setViewPager(int fragmentNumbers){
mRelativeLayout.setVisibility(View.GONE);
Log.d(TAG,"setViewPager: navigating to fragment #:" + fragmentNumbers);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(fragmentNumbers);
this is my AccountSettingActivity
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
public class AccountSettingActivity extends AppCompatActivity {
private Context mContext;
private SectionStatePagerAdapter pagerAdapter;
private ViewPager viewPager;
private RelativeLayout mRelativeLayout;
private String TAG;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) findViewById(R.id.container);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relaytiveLayout11);
mContext = AccountSettingActivity.this;
setContentView(R.layout.activity_accountsettings);
setupSettingList();
setupFragment();
ImageView backArrow = (ImageView) findViewById(R.id.backArrow);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void setupFragment(){
pagerAdapter = new SectionStatePagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(new EditProfileFragment(), getString(R.string.edit_profile_fragment)); //fragment 0
pagerAdapter.addFragment(new SignOutFragment(), getString(R.string.sign_out_fragment)); //fragment 1
}
public void setViewPager(int fragmentNumbers){
mRelativeLayout.setVisibility(View.GONE);
Log.d(TAG,"setViewPager: navigating to fragment #:" + fragmentNumbers);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(fragmentNumbers);
}
private void setupSettingList(){
ListView listView = (ListView) findViewById(R.id.lvAccountSettings);
ArrayList<String> options = new ArrayList<>();
options.add(getString(R.string.edit_profile_fragment)); //fragment 0
options.add(getString(R.string.sign_out_fragment)); //fragement 1
ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, options);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setViewPager(position);
}
});
}
}
this is my xml file
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/layout_center_viewpager"></include>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relaytiveLayout11">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout2">
<include layout="@layout/snippet_top_settingtoolbar"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relLayout3"
android:layout_below="@id/relLayout2"
android:layout_marginBottom="50dp">
<include layout="@layout/layout_center_accountsettiings"/>
</RelativeLayout>
</RelativeLayout>
i am using android x library may i am getting error because of this library
I am unable to find the error in this code I did a lot of experiment on it but nothing works out I am sure some of you would help me out THANKS IN ADVANCE
Do not call any findViewById
i.e. mRelativeLayout = (RelativeLayout) findViewById(R.id.relaytiveLayout11);
before calling setContentView
. Make sure you call it before accessing any view.
Reorient your code like this
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = AccountSettingActivity.this;
setContentView(R.layout.activity_accountsettings);
viewPager = (ViewPager) findViewById(R.id.container);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relaytiveLayout11);
setupSettingList();
setupFragment();
ImageView backArrow = (ImageView) findViewById(R.id.backArrow);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}