Unable to start activity ComponentInfo Java.lang.nullExeption - java

I'm new in android and I have a program that gives data from localhost via HTTP and parse it to XML and show it in a list in android.But I can't switch to new page(the page that shows the list).My program has java.lang.NullPointerException exception and I can't found where It gives null.please help me and tell me what should I do.
this is error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sara.patientapplication/com.example.sara.patientapplication.ViewAllPatientActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.sara.patientapplication.ViewAllPatientActivity.onCreate(ViewAllPatientActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)at dalvik.system.NativeStart.main(Native Method)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)at android.app.ActivityThread.access$600(ActivityThread.java:130)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4745)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
this is my main layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".mainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show All Patients"
android:id="#+id/ShowAllPatients"
android:layout_marginTop="25dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Patient"
android:id="#+id/AddNewP"
android:layout_below="#+id/ShowAllPatients"
android:layout_marginTop="10dp"/>
</RelativeLayout>
and this is main Activity.there are a button and when I press it I expect go to a list but at that time exception Occurs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button ShowAllPatient=(Button)findViewById(R.id.ShowAllPatients);
Button AddNewPatient=(Button)findViewById(R.id.AddNewP);
ShowAllPatient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),ViewAllPatientActivity.class);
startActivity(i);
}
});
}
this my list layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"></ListView>
</LinearLayout>
and this is my the list activity:
public class ViewAllPatientActivity extends ListActivity {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser=new XMLParser();
static final String URL="http://localhost/AllPatient.php";
static final String Patient_ID="PatientId";
static final String First_Name="PatientFirstName";
static final String Last_Name="PatientLastName";
//static final String Blood_Type="BloodType";
//static final String Phone_Number="phoneNumber";
//static final String Practitioner_ID="practitionerId";
//static final String Email="Email";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_patient);
// String xml=parser.GetXml(URL);
Document doc=parser.DomElement("http://localhost/AllPatient.php");
NodeList nl = doc.getElementsByTagName(Patient_ID);
for (int i=0;i<nl.getLength();i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(Patient_ID,parser.GetValue(e , Patient_ID));
map.put(First_Name,parser.GetValue(e,First_Name));
map.put(Last_Name,parser.GetValue(e,Last_Name));
// map.put(Blood_Type,parser.GetValue(e,Blood_Type));
// map.put(Phone_Number,parser.GetValue(e,Phone_Number));
// map.put(Email,parser.GetValue(e,Email));
// map.put(Practitioner_ID,parser.GetValue(e,Practitioner_ID));
menuItems.add(map);
}
ListAdapter adapter=new SimpleAdapter(this,menuItems,R.layout.list_item,
new String[]{Patient_ID,First_Name,Last_Name},new int[]{R.id.pid,R.id.fName,R.id.lName});
setListAdapter(adapter);
ListView lv=getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Patient_id = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String First_name = ((TextView) view.findViewById(R.id.fName)).getText().toString();
String Last_name = ((TextView) view.findViewById(R.id.lName)).getText().toString();
Intent in = new Intent(getApplicationContext(), SingleListPatient.class);
in.putExtra(Patient_ID, Patient_id);
in.putExtra(First_Name, First_name);
in.putExtra(Last_Name, Last_name);
startActivity(in);
}
});
}
and also I have some layout for a single list Item:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" /><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" /></LinearLayout>
and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/lastN"
android:layout_toRightOf="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Patient ID: "
android:id="#+id/paId"
android:layout_below="#+id/firstN"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/patientid"
android:layout_toRightOf="#+id/paId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Blood Type: "
android:id="#+id/bt"
android:layout_below="#+id/paId"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/btype"
android:layout_toRightOf="#+id/bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Phone Number: "
android:id="#+id/pn"
android:layout_below="#+id/bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/pnum"
android:layout_toRightOf="#+id/pn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Email: "
android:id="#+id/em"
android:layout_below="#+id/pn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="#+id/mail"
android:layout_toRightOf="#+id/em"/>
</RelativeLayout>
please help me.
thanks.

Change:
Intent i=new Intent(getApplicationContext(),ViewAllPatientActivity.class);
startActivity(i);
To:
Intent i=new Intent( mainActivity.this,ViewAllPatientActivity.class);
startActivity(i);
And check if:
Document doc is null

Related

onTouchListener for custom view keeps crashing my app [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm currently making a soundboard app for Android with custom neomorphic buttons. My goal for the app is for the button to change style (look like it's pressed) when I click it using an onTouchListener. However my app crashes whenever I try to add an onTouchListener (in the initButton() function on MainAcitvity.java). I attached my code as well as links to the neomrphic library I used below:
Neomorphic View Library:
https://medium.com/#fornewid/neumorphism-in-android-9cf15e2122dc
https://github.com/fornewid/neumorphism
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.titleText);
String text = "R6 Soundboard";
SpannableString ss = new SpannableString(text);
ForegroundColorSpan fcsYellow = new ForegroundColorSpan(Color.rgb(255,236,141));
ss.setSpan(fcsYellow, 1,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.view_pager);
setUpViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabTextColors(Color.WHITE, Color.WHITE);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setText("Defenders");
tabLayout.getTabAt(1).setText("Attackers");
initButton();
}
private void setUpViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new defendersTab(), "Defenders");
adapter.addFragment(new attackersTab(), "Attackers");
viewPager.setAdapter(adapter);
}
#SuppressLint("ClickableViewAccessibility")
private void initButton(){
final NeumorphCardView DefR1C1 = (NeumorphCardView) findViewById(R.id.buttonDR1C1);
DefR1C1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
DefR1C1.setShapeType(ShapeType.PRESSED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
DefR1C1.setShapeType(ShapeType.FLAT);
}
return false;
}
});
/*
final MediaPlayer mp = MediaPlayer.create(this, R.raw.lionroar);
DefR1C1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if(mp.isPlaying()) {
//STOP PLAYING
mp.seekTo(0);
}
mp.start();
return false;
}
});
*/
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/titleText"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/avenir"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="R6 Soundboard"
android:textColor="#FFF"
android:textSize="30sp" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#fff"
android:background="?attr/colorPrimaryDark" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
fragment_defenders_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR1C1"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Lion Scan"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR1C2"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Thermite"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR2C1"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_below="#+id/buttonDR1C2"
android:layout_alignParentEnd="true"
android:layout_marginTop="11dp"
android:layout_marginEnd="20dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Barricade"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR2C2"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_below="#+id/buttonDR1C1"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="12dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Planting"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR3C1"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_below="#+id/buttonDR2C2"
android:layout_alignParentEnd="true"
android:layout_marginTop="11dp"
android:layout_marginEnd="20dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Fuze"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:id="#+id/buttonDR3C2"
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_below="#+id/buttonDR2C1"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="12dp"
app:neumorph_backgroundColor="#color/colorPrimaryDark"
app:neumorph_inset="12dp"
app:neumorph_insetBottom="12dp"
app:neumorph_insetEnd="12dp"
app:neumorph_insetStart="12dp"
app:neumorph_insetTop="12dp"
app:neumorph_lightSource="leftTop"
app:neumorph_shadowColorDark="#color/solid_dark_color"
app:neumorph_shadowColorLight="#color/solid_light_color"
app:neumorph_shadowElevation="3dp"
app:neumorph_shapeType="flat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="62.5dp"
android:fontFamily="#font/proximanovasoft"
android:text="Drone"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
</RelativeLayout>
</FrameLayout>
SectionsPageAdapter.java
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return super.getPageTitle(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
edit: on request, I added the crash log and a picture of my app working before I tried to make the buttons actually work using onTouchListener
2020-12-14 10:54:31.781 8217-8217/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jkcarraher.rainbowsixsoundboard, PID: 8217
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jkcarraher.rainbowsixsoundboard/com.jkcarraher.rainbowsixsoundboard.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void soup.neumorphism.NeumorphCardView.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void soup.neumorphism.NeumorphCardView.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
at com.jkcarraher.rainbowsixsoundboard.MainActivity.initButton(MainActivity.java:70)
at com.jkcarraher.rainbowsixsoundboard.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Thank you for any insight you can provide!
Your NeumorphCardView is not initialized correctly, and that why it is throwing a NullPointerException. My guess is that you didn't set up your TabLayout in your activity_main.xml correctly. Specifically, what is your android:id="#+id/tabs" referring to? I don't think you successfully attached your fragment_defenders_tab to your activity_main successfully.
Your error is that your are using activity_main.xml in your MainActivity.java. However, the XML file you provided is called fragment_defenders_tab. You didn't reference this at all in your code.
So, the simple fix should be to just change this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
To this:
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_defenders_tab);

NullPointerException on defined TextView

I am working on an app using a CardView menu & I am running into some troubles.
The CardView menu presents different tools & among those, there is "location" which uses a third-party library that I used very recently to retrieve the longitude and latitude.
When the user clicks into this card "location", a popup shows up & after pressing the button "update", I set two textviews in that popup to the retrieved longitude and latitude.
However, I seem to run into a NullPointerException when I try:
longitudeTv.setText("Longitude: " + deviceLongitude);
Here are the main parts of my code:
The Menu Class - the launcher activity:
public class Menu extends AppCompatActivity {
GridLayout menuGrid;
SimpleLocation myLocation;
public static double deviceLongitude;
public static double deviceLatitude;
public static Dialog infoPopupDialog;
static TextView messageTv;
public static Dialog locationPopupDialog;
public static TextView longitudeTv;
public static TextView latitudeTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
menuGrid = (GridLayout) findViewById(R.id.menuGrid);
myLocation = new SimpleLocation(this);
infoPopupDialog = new Dialog(this);
messageTv = (TextView) findViewById(R.id.messageTv);
locationPopupDialog = new Dialog(this);
longitudeTv = (TextView) findViewById(R.id.longitudeTv);
latitudeTv = (TextView) findViewById(R.id.latitudeTv);
// if we can't access the location yet
if (!myLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
CardView dataCard = (CardView) findViewById(R.id.dataCard);
CardView locationCard = (CardView) findViewById(R.id.locationCard);
CardView timeCard = (CardView) findViewById(R.id.timeCard);
CardView websiteCard = (CardView) findViewById(R.id.websiteCard);
CardView emailCard = (CardView) findViewById(R.id.emailCard);
CardView infoCard = (CardView) findViewById(R.id.infoCard);
infoCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.showInfoPopup();
}
});
dataCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent dataActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dataActivity);
}
});
locationCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final double longit = roundCoordinates(myLocation.getLongitude());
final double latit = roundCoordinates(myLocation.getLatitude());
deviceLongitude = longit;
deviceLatitude = latit;
MainActivity.showLocationPopup();
}
});
timeCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Menu.this, "Next update in 11 mns", Toast.LENGTH_SHORT).show();
}
});
websiteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://159.203.78.94/rpilog/weatherstation.txt")); //Insert website.
startActivity(openWebsite);
}
});
emailCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Weather Station Data Report");
intent.putExtra(Intent.EXTRA_TEXT, MainActivity.dataFromURL); //Add string variable holding entire data here.
intent.setData(Uri.parse("mailto:RHellstrom#bridgew.edu"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
}
});
}
//function to round to 2 decimal places our GPS coordinates.
public static double roundCoordinates(double coordinate){
String result = String.format("%.2f", coordinate);
double roundedValue = Double.parseDouble(result);
return roundedValue;
}
public void closePopup(View v){ //ONCLICK OF CLOSE ICON
infoPopupDialog.dismiss();
locationPopupDialog.dismiss();
}
public void updateCoordinates(View v){ //ONCLICK BTN "UPDATE"
Menu.latitudeTv.setText("Latitude: " + Menu.deviceLatitude);
Menu.longitudeTv.setText("Longitude: " + Menu.deviceLongitude);
}
}
Menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapps.toualbiamine.weathertracker.Menu"
android:background="#drawable/bg"
android:weightSum="10"
android:orientation="vertical"
android:backgroundTintMode="multiply"
android:backgroundTint="#color/background"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather Tracker"
android:textSize="34sp"
android:textColor="#color/titleColor"
android:layout_centerInParent="true"/>
</RelativeLayout>
<GridLayout
android:id="#+id/menuGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3"
>
<!-- Row 1 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/dataCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/data"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="32dp"
android:text="Data"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/locationCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="29dp"
android:text="Location"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/timeCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/time"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Update Time"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/emailCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/email"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="35dp"
android:text="Email"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/websiteCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/web"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Website"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/infoCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info"
android:layout_gravity="center_horizontal"
android:layout_marginRight="17dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="34dp"
android:text="Info"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
Popup_location.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
<ImageView
android:id="#+id/closePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_alignParentRight="true"
android:elevation="5dp"
android:layout_marginTop="7dp"
android:layout_marginRight="7dp"
android:onClick="closePopup"/>
<android.support.v7.widget.CardView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
app:cardBackgroundColor="#color/background"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/location_popup"
/>
<TextView
android:id="#+id/longitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Longitude: "
/>
<TextView
android:id="#+id/latitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Latitude: "
/>
<Button
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="UPDATE"
android:textColor="#color/colorWhite"
android:background="#drawable/update_btn_circle"
android:layout_gravity="center_horizontal"
android:onClick="updateCoordinates"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapps.toualbiamine.weathertracker, PID: 24053
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.
onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.
run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller
.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at
com.myapps.weathertracker.Menu.updateCoordinates(Menu.java:149)
So, the problematics textviews are defined in the popup.xml.
Please, help. I tried, I searched. I can't find anything.
Community, you're my last hope.
PSA: I also have 3 other fragment classes, a viewpager and a a class retrieving data from a URL using the Volley library by Google but they don't seem to coincide with the problem here. Let me know if you need them.
If the views are in your dialog you have to call findViewById on the dialog, as in:
longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);
They can't be found in the menu activity and are null.
You can't call this right away though, it has to be after you have set the layout of the dialog. However, I don't see where you actually set the layout of the dialog to popup_location.xml in the code you posted. Presumably that's in the showLocationPopup method.
The longitudeTv is childview of locationPopupDialog,you need set the layout of the dialog (popup_location.xml). then init with longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);

navigate to another page using the button: Unable to start activity ComponentInfo java.lang.NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm developing a demo app when i login it will navigate to another page.
Here using button i'm trying to navigate to other page
Main activity
public class WelcomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button ibsignin = (Button) findViewById(R.id.ibsignin);
ibsignin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (v.getContext(), Register.class);
startActivity(intent);
}
});
}
}
I have defined button code in xml file still i'm getting null pointer exception.
XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.nandan.uschedule.WelcomeActivity"
android:background="#drawable/edit1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:id="#+id/welcome"
android:background="#drawable/minnesota"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:clickable="false"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="#+id/ibusername"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_marginBottom="200dp"
android:layout_marginTop="225dp"
android:inputType="textEmailAddress|text"
android:hint="User name/ Email Address"
android:textColor="#000000"
android:textStyle="normal" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:inputType="textPassword|text"
android:ems="10"
android:id="#+id/ibpassword"
android:hint="Password"
android:layout_gravity="center"
android:layout_marginTop="-200dp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:id="#+id/cbRememberme"
android:hint="Remember me"
android:layout_gravity="center" />
<Button
android:layout_width="250dp"
android:layout_height="38dp"
android:text="Sign in"
android:id="#+id/ibsingin"
android:layout_gravity="center"
android:background="#4D306A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Do not have an account yet?"
android:id="#+id/havenoaccount"
android:layout_gravity="center"
android:textColor="#000000"
android:paddingTop="12dp"
android:textSize="20dp" />
<Button
android:layout_width="250dp"
android:layout_height="38dp"
android:text="Sign up"
android:id="#+id/ibsignup"
android:layout_gravity="center"
android:background="#4D306A" />
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgot Password?"
android:id="#+id/ibforgotpwd"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="20dp"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
my logcat
Process: com.example.nandan.uschedule, PID: 2487
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nandan.uschedule/com.example.nandan.uschedule.WelcomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.nandan.uschedule.WelcomeActivity.onCreate(WelcomeActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I don't know which line is 27, but it's most likely the NPE comes from here:
Button ibsignin = (Button) findViewById(R.id.ibsignin);
Make sure your button is in your XML layout and that it is called ibsignin.

Error inflating fragment xml

I'm trying to update a textview in a fragment based on a passed intent, but I keep getting an error. Here is my code:
public class PlayerDisplayFragment extends Fragment {
public PlayerDisplayFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_player_display, container, false);
String[] playerArray = getArguments().getStringArray("playerArray");
updateText(view,playerArray);
return view;
}
public void updateText (View view , String[] playerArray){
String pName = playerArray[0] + " " + playerArray[1];
TextView playerName = (TextView) view.findViewById(R.id.player_name);
playerName.setText(pName);
}
}
The code crashes when it tries to execute the updateText function. Here is the xml element (in the fragment xml file) that it refers to:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Placeholder"
android:textSize="18sp"
android:id="#+id/player_name"
android:gravity="center">
</TextView>
And lastly, here is the error:
26609-26609/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.soccerstar, PID: 26609
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.soccerstar/com.example.android.soccerstar.PlayerDisplay}:
android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2172)
at com.example.android.soccerstar.PlayerDisplay.onCreate(PlayerDisplay.java:13)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
edit: full xml here:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.soccerstar.PlayerDisplayFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="#drawable/placeholder_m"
android:background="#7b7b7b"
android:layout_alignParentTop="true"
android:id="#+id/PlayerPhoto"/>
/>
<TextView
android:layout_width="match_parent"
android:layout_height="58dp"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:text="You matched Lionel Messi!"
android:layout_below="#+id/PlayerPhoto"
android:id="#+id/MatchText">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_below="#+id/MatchText"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textSize="18sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Born on April 15, 1992"
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Is 5 foot 11 inches tall."
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Weighs 160 lbs."
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Prefers right foot"
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="American Nationality"
android:textSize="14sp"
android:gravity="center">
</TextView>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="300dp"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lionel Messi"
android:textSize="18sp"
android:id="#+id/player_name"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Born on April 15, 1992"
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Is 5 foot 11 inches tall."
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Weighs 160 lbs."
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="Prefers right foot"
android:textSize="14sp"
android:gravity="center">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:layout_margin="8dp"
android:text="American Nationality"
android:textSize="14sp"
android:gravity="center">
</TextView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
 
Here is from the main activity:
public class PlayerDisplay extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_display); <--- THIS IS LINE 13
}
Here is the activity_player_display.xml file:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.example.android.soccerstar.PlayerDisplayFragment"
tools:layout="#layout/fragment_player_display" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayerDisplay" />
Where I feed in the array from the main fragment:
Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class);
intent.putExtra("playerArray", playerArray);
startActivity(intent);
Use the Bundle in your Main Fragment like this.
Fragment fragment = new PlayerFragment();
Bundle bundle = new Bundle();
bundle.putStringArray("playerArray", playerArray);
fragment.setArguments(bundle);
And in your Player Fragment getArguments.
Bundle bundle = this.getArguments();
if(bundle != null){
String[] playerArray = getArguments().getStringArray("playerArray");
}
Hope this will help you.
Finally figured it out. It was so simple!
That line 13 code that was being referenced, it was from the main activity of the fragment. It was referencing the main activity xml file instead of the fragment xml file. So I changed it:
public class PlayerDisplay extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_display); <--- THIS IS LINE 13
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_player_display); <--- THIS IS LINE 13
}
And now it works! Thanks all.

Error Inflating class android.support.design.widget.NavigationView [Crash on Startup]

The app is supposed to have a navigation drawer that pulls out from the left and shows the various activities, but once the navigation bar is added to the XML activity_homescreen doc, the app crashes as soon as it starts.
HomeScreen.java
package com.t99sdevelopment.centralized;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toolbar;
public class HomeScreen extends AppCompatActivity {
/*
Intent intentHome = new Intent(this, HomeScreen.class);
Intent intentAnnouncements = new Intent(this, AnnouncementsScreen.class);
Intent intentSchedule = new Intent(this, ScheduleScreen.class);
Intent intentCalendar = new Intent(this, CalendarScreen.class);
Intent intentContactBook = new Intent(this, ContactBookScreen.class);
Intent intentSportsSchedule = new Intent(this, SportsScheduleScreen.class);
Intent intentFrontAndCentral = new Intent(this, FrontAndCentralScreen.class);
Intent intentMap = new Intent(this, MapScreen.class);
Intent intentAccount = new Intent(this, AccountScreen.class);
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homescreen);
setTheme(R.style.AppTheme);
}
/*
private void goToHome(View view){ startActivity(intentHome ); }
private void goToAnnouncements(View view){ startActivity(intentAnnouncements ); }
private void goToSchedule(View view){ startActivity(intentSchedule); }
private void goToCalendar(View view){ startActivity(intentCalendar); }
private void goToContactBook(View view){ startActivity(intentContactBook); }
private void goToSportsSchedule(View view){ startActivity(intentSportsSchedule); }
private void goToFrontAndCentral(View view){ startActivity(intentFrontAndCentral); }
private void goToMap(View view){ startActivity(intentMap); }
private void goToAccount(View view){ startActivity(intentAccount); }
*/
}
activity_homescreen.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#android:style/Theme.DeviceDefault.Light.NoActionBar"
android:background="#color/trojanBlack">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/actionbar" />
<ImageView
android:id="#+id/ImageView_trojanHead"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="150dp"
android:src="#drawable/trojan"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="100dp"
android:text="School is"
android:paddingTop="5dp"
android:textSize="30sp"
android:textColor="#color/trojanBlack"
android:textAlignment="center"
android:background="#drawable/schoolbutton"
android:radius="5dp"/>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="-100dp">
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/refresh"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:text="OPEN"
android:textSize="40sp"
android:textColor="#color/trojanBlack"
android:textAlignment="center"
android:paddingTop="45dp" />
</RelativeLayout>
<TextView
android:layout_width="250dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/navigationdraweritems"
app:headerLayout="#layout/nav_header" />
</android.support.v4.widget.DrawerLayout>
With these files as is, the app crashes, but if I change the activity_homescreen.xml as follows (commenting out the navigation view widget)...
activity_homescreen.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#android:style/Theme.DeviceDefault.Light.NoActionBar"
android:background="#color/trojanBlack">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/actionbar" />
<ImageView
android:id="#+id/ImageView_trojanHead"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="150dp"
android:src="#drawable/trojan"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="100dp"
android:text="School is"
android:paddingTop="5dp"
android:textSize="30sp"
android:textColor="#color/trojanBlack"
android:textAlignment="center"
android:background="#drawable/schoolbutton"
android:radius="5dp"/>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="-100dp">
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/refresh"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:text="OPEN"
android:textSize="40sp"
android:textColor="#color/trojanBlack"
android:textAlignment="center"
android:paddingTop="45dp" />
</RelativeLayout>
<TextView
android:layout_width="250dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!--
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/navigationdraweritems"
app:headerLayout="#layout/nav_header" />
-->
</android.support.v4.widget.DrawerLayout>
Everything works just fine, with the exception of the navigation drawer not being included/working. Has anyone had this issue, or even better, does anyone know how to fix it? I need the navigation drawer working, but it seems to crash my app.
The logcat spilled over the maximum number of characters, so here's the github gist.
Most definitely a problem with a missing method, from your logcat :
Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler goToHome in class com.t99sdevelopment.centralized.HomeScreen
Caused by: java.lang.NoSuchMethodException: goToHome [interface android.view.MenuItem]

Categories

Resources