I was making a list of favorite activity in the android which uses arraylist
as a storing place,but when i was triggering the event by the button press,null pointer error is coming. Why is it so...???
Here is the code of my Kinematics activity where the button is being pressed.
public class Kinematics extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle phy_law_toggel;
private Toolbar toolbar;
private WebView webView;
private ScrollView scrollview;
private FloatingActionButton fab;
private ImageButton fav;
@Override
protected void onCreate(Bundle savedInstanceState) {
final Favouritepage favr = new Favouritepage();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kinematics);
Toolbar toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
setSupportActionBar(toolbar);
setTitle(R.string.Kinematics);
drawerLayout = (DrawerLayout) findViewById(R.id.phy_draw);
navigationView = (NavigationView) findViewById(R.id.nav_view_phy);
phy_law_toggel = new ActionBarDrawerToggle(this,drawerLayout,R.string.open, R.string.Close);
drawerLayout.addDrawerListener(phy_law_toggel);
toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
setSupportActionBar(toolbar);
phy_law_toggel.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setItemIconTintList(null);
webView = (WebView) findViewById(R.id.phy_law_web);
fav = (ImageButton) findViewById(R.id.fav);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
final String kine = this.getClass().getSimpleName();
webView.getSettings().setDisplayZoomControls(true);
webView.loadUrl("file:///android_asset/mathscribe/Kinematics.html");
fab = (FloatingActionButton) findViewById(R.id.fab);
scrollview = (ScrollView) findViewById(R.id.scroll);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.scrollTo(0, 0);
}
});
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
favr.mfavlist.add(kine);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (phy_law_toggel.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}}
Here, i am making an object of my FavoritePage class and trying to store the element in the arrayList of FavoritePage.
Here is my FavoritePage class
public class Favouritepage extends AppCompatActivity {
public ArrayList<String> mfavlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favouritepage);
mfavlist = new ArrayList<String>();
}}
And here is the XML of kinematics,
<android.support.v4.widget.DrawerLayout
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:id="@+id/phy_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.alpit.formula2.Kinematics">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/phy_lawtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay">
</android.support.v7.widget.Toolbar>
<WebView
android:id="@+id/phy_law_web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/phy_lawtoolbar"></WebView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignBottom="@+id/phy_law_web"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_marginBottom="43dp"
android:layout_marginEnd="30dp"
android:src="@drawable/fabss"
app:backgroundTint="#F57F17">
</android.support.design.widget.FloatingActionButton>
<ImageButton
android:id="@+id/fav"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="@+id/phy_law_web"
android:layout_alignParentEnd="true"
android:background="@color/colorPrimary"
android:src="@drawable/fav" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_phy"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFAB40"
app:menu="@menu/navigation_menu"
app:theme="@style/NavigationTheme" />
crashlog
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alpit.formula2, PID: 22744
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.alpit.formula2.Kinematics$2.onClick(Kinematics.java:64)
at android.view.View.performClick(View.java:4802)
at android.view.View$PerformClick.run(View.java:20102)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)