Error inflating fragment xml - java

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.

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);

TextView Null Pointer Exception after initialising also

public class PostDetails extends AppCompatActivity {
TextView like,dislike,date,time,name,username;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private ArrayList<PostInfo> postInfos;
int i;
ImageView glideImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
i = getIntent().getIntExtra("intVariableName", 0);
postInfos = new ArrayList<>();
like = findViewById(R.id.like);
dislike = findViewById(R.id.dislike);
name = findViewById(R.id.name);
username = findViewById(R.id.username);
date= findViewById(R.id.date);
time = findViewById(R.id.time);
glideImage = findViewById(R.id.glideImage);
getData();
}
public void getData(){
reference.addValueEventListener (new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot posts = dataSnapshot.child ("Posts");
for (DataSnapshot time: posts.getChildren ()){
DataSnapshot url = time.child ("Url");
DataSnapshot name = time.child ("Name");
DataSnapshot username = time.child ("Username");
DataSnapshot date = time.child ("Date");
DataSnapshot likes = time.child("Likes");
DataSnapshot times = time.child("Time");
DataSnapshot dislikes = time.child("DisLikes");
PostInfo postInfo = new PostInfo (String.valueOf (url.getValue ())
,String.valueOf (name.getValue ())
,String.valueOf (username.getValue ())
,String.valueOf (date.getValue ())
,time.getKey()
,String.valueOf(likes.getValue())
,String.valueOf(dislikes.getValue())
,String.valueOf(times.getValue()));
postInfos.add (postInfo);
}
like.setText(postInfos.get(i).likes);
dislike.setText(postInfos.get(i).dislikes);
name.setText(postInfos.get(i).name);
username.setText(postInfos.get(i).username);
date.setText(postInfos.get(i).date);
time.setText(postInfos.get(i).time);
Glide.with(getApplicationContext()).load(postInfos.get(i).Url).into(glideImage);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (PostDetails.this, "Error 411: " + databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
Here are six TextView which are initialised but when I try to write the values using setText, it shows this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at lifeline.learn.com.hotornot.PostDetails$1.onDataChange(PostDetails.java:67)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zzcal(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
I tried many things to get rid of this exception but fails every time. I am new to Android Programming so help me out of this exception. I filtered many questions on overflow but doesnt find any appropriate answer which can solve this exception.Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".PostDetails">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:id="#+id/glideImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/glideImage"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/likes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/like"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:text="#string/dislikes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/dislike"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/date"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="#string/time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="#string/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
In your XML, there is no id,like this: android:id="#+id/fullscreen_content"
as you know findViewById may return null,please check your view initialize...
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {#link #onCreate}.
*
* #return The view if found or null otherwise.
*/
#SuppressWarnings("TypeParameterUnusedInFormals")
#Nullable
public abstract <T extends View> T findViewById(#IdRes int id);
Demo:
<?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"
android:background="#0099cc"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:baselineAligned="false"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:id="#+id/fullscreen_choose_controls"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/fullscreen_esop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_esop"
android:textAllCaps="false"
android:textColor="#android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:ellipsize="end"
android:gravity="center"
android:text="#string/dummy_button"
android:textColor="#android:color/holo_red_dark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/fullscreen_android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_android"
android:textAllCaps="false"
android:textColor="#android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/fullscreen_make_your_choice"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_make_your_choice"
android:textColor="#android:color/holo_red_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</FrameLayout>

Could not find method onClickcrea(View) in a parent or ancestor Context for android:onClick

I basically can't understand when i click on the onClick="onClickcrea" it doesn't works. I also tried changing the name of the onClick but it doesn't matter. I get this error:
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'onClickcrea'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
here is my xml activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="purgimon.pt11_marc_purgimon.Registra"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/botoUsuari"
android:id="#+id/NUsuari"
android:layout_weight="0.11"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/botoCorreu"
android:id="#+id/Correu"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="32dp"
android:id="#+id/editText3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="34dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/botoTelefon"
android:id="#+id/Correu"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="32dp"
android:id="#+id/editText4" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.03"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/botoContrasenya"
android:id="#+id/Contrasenya"
android:textSize="20dp"
android:layout_weight="0.05" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.57">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editTextContra" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.03" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/botoRepeteixContrasenya"
android:id="#+id/ContrasenyRepetida"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextContraR" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/Botosexe"
android:id="#+id/Sexe"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.31"
android:weightSum="1">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/BotoHome"
android:id="#+id/Home"
android:checked="false"
android:layout_weight="0.08" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/botoDona"
android:id="#+id/Dona"
android:layout_weight="0.12"
android:checked="false"
android:layout_marginLeft="150dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/boto2Ajuda"
android:id="#+id/AjudaRegistra"
android:layout_marginLeft="30dp"
android:onClick="ajuda"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/botoCrea"
android:id="#+id/onClickcrea"
android:layout_marginLeft="120dp"
android:onClick="onClickcrea" />
</LinearLayout>
</LinearLayout>
And here my Java code:
public class Registra extends AppCompatActivity {
String contrasenya1;
String contrasenya2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registra);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_principal, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void ajuda(View v) {
Intent intent = new Intent(this, Ajuda.class);
startActivity(intent);
}
public void onClickcrea (View view){
EditText textContra1 =(EditText) findViewById(R.id.editTextContra);
contrasenya1 = textContra1.getText().toString();
EditText textContra2 = (EditText) findViewById(R.id.editTextContraR);
contrasenya2 = textContra2.getText().toString();
Toast missatge = Toast.makeText(getApplicationContext(), "Les contrasenyes han de coincidir!", Toast.LENGTH_SHORT);
missatge.show();
}
}
Personally I would suggest using a onClickListener instead of the XML attribute.
The method defined in the android:onClick could not be found in the activity that the view was inflated in.
The most common mistakes for this error are method name typo and wrong layout inflation.
As stated in other comments you should rather use onClickListeners.

not getting the parent view elements in adapter

I have an adapter called PaymentMethodAdapter. I am trying to access it's parent view element, which is already there in xml.
ViewGroup parentView;
LinearLayout listView;
public View getView(int position, View convertView, ViewGroup parent) {
parentView = parent;
listView = (LinearLayout) parentView.findViewById(R.id.ben_pay_methods);
}
I am getting nullpointer exception error:
TRACE
01-20 13:22:47.075 6569-6569/com.example.android.mtesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.mtesapp, PID: 6569
java.lang.NullPointerException
at com.example.android.mtesapp.PaymentMethodAdapter.getView(PaymentMethodAdapter.java:65)
at com.example.android.mtesapp.EditBeneficiaryActivity.show_receiving_methods(EditBeneficiaryActivity.java:315)
at com.example.android.mtesapp.EditBeneficiaryActivity$AsyncTaskGetBenPaymentMethods.onPostExecute(EditBeneficiaryActivity.java:299)
at com.example.android.mtesapp.EditBeneficiaryActivity$AsyncTaskGetBenPaymentMethods.onPostExecute(EditBeneficiaryActivity.java:283)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
How can I check the elements in the parent view exist or not?
EDIT
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
style="#style/panelLayout"
android:background="#color/white"
android:id="#+id/panel1"
>
<RelativeLayout
style="#style/panelFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_height="120dp"
android:layout_width="120dp"
android:id="#+id/ben_pic"
android:src="#drawable/bee"
android:background="#color/white"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="John Doe"
android:id="#+id/ben_first_name"
android:layout_alignTop="#+id/ben_pic"
android:layout_toRightOf="#+id/ben_pic"
android:layout_toEndOf="#+id/ben_pic"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:hint="First Name"
android:background="#drawable/blue_edit_text_holo_light"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="John Doe"
android:id="#+id/ben_last_name"
android:layout_below="#+id/ben_first_name"
android:layout_toRightOf="#+id/ben_pic"
android:layout_toEndOf="#+id/ben_pic"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:hint="Last Name"
android:background="#drawable/blue_edit_text_holo_light"
/>
<!-- <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="someone#something.com"
android:id="#+id/ben_email"
android:hint="#string/email"
android:layout_below="#+id/ben_last_name"
android:layout_toRightOf="#+id/ben_pic"
android:layout_toEndOf="#+id/ben_pic"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#drawable/blue_edit_text_holo_light"/>-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="647-777-0000"
android:hint="#string/phone"
android:id="#+id/ben_phone1"
android:layout_below="#+id/ben_last_name"
android:layout_toRightOf="#+id/ben_pic"
android:layout_toEndOf="#+id/ben_pic"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#drawable/blue_edit_text_holo_light"/>
<Button
style="#style/BtnDanger"
android:layout_width="wrap_content"
android:text="Delete"
android:id="#+id/delete_ben_btn"
android:layout_below="#+id/ben_phone1"
android:layout_toRightOf="#+id/ben_pic"
android:layout_toEndOf="#+id/ben_pic"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
style="#style/panelLayout"
android:id="#+id/panel2">
<TextView
style="#style/title_inside"
android:text="#string/address" />
<EditText
style="#style/Base.Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ben_address1"
android:text="123 Something Ave"
android:hint="#string/address"
android:background="#drawable/blue_edit_text_holo_light">
</EditText>
<!--<TextView
style="#style/LabelEdit"
android:text="#string/address" />-->
<EditText
style="#style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ben_address2"
android:text=""
android:hint="#string/address2"
android:background="#drawable/blue_edit_text_holo_light">
</EditText>
<EditText
style="#style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ben_city"
android:text="Toronto"
android:hint="#string/city"
android:background="#drawable/blue_edit_text_holo_light">
</EditText>
<EditText
style="#style/Widget.AppCompat.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ben_province"
android:text="Ontario"
android:hint="#string/province"
android:background="#drawable/blue_edit_text_holo_light">
</EditText>
<TextView
style="#style/Widget.AppCompat.EditText"
android:id="#+id/ben_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/country"
android:background="#drawable/blue_edit_text_holo_light"/>
</LinearLayout>
<LinearLayout
style="#style/panelLayout"
android:id="#+id/panel3">
<TextView
style="#style/title_inside"
android:text="#string/receive_method" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/ben_pay_methods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout1"
>
</LinearLayout>
<Button
style="#style/BtnInfo"
android:id="#+id/btn_add_new_method"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="add_new_method"
android:text="#string/add_new_method"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:layout_alignParentBottom="true">
<Button
style="#style/BtnPrimary"
android:id="#+id/btn_edit_beneficiary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/save"/>
</LinearLayout>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
As #Fouad Wahabi has already suggested in the comment, I found your layout XML is invalid.
There is no View with id of layout1. And also, though the LinearLayout is not a child View of RelativeLayout, it has an android:layout_below attibute. This can cause the error.
Try to remove the attribute like this:
<LinearLayout
android:orientation="vertical"
android:id="#+id/ben_pay_methods"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
use convertView for inflating any view from item view of list
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.your_xml, null);
listView = (LinearLayout) convertView.findViewById(R.id.ben_pay_methods);
}

Unable to start activity ComponentInfo Java.lang.nullExeption

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

Categories

Resources