将活动放入收藏页面时出错

时间:2017-02-24 15:14:48

标签: android nullpointerexception android-vectordrawable

我有一个矢量可绘图像。它的用途是,只要点击它,活动就会被添加到favourite页面。

这是我的kinematics.java类

protected void onCreate(Bundle savedInstanceState) {

    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);
    webView.getSettings().setDisplayZoomControls(true);
    webView.loadUrl("file:///android_asset/mathscribe/Kinematics.html");

    final ArrayList<String> mfavlist = new ArrayList<String>();
    final Favouritepage favr = new Favouritepage();
    kine = this.getClass().getSimpleName();
    fab = (FloatingActionButton) findViewById(R.id.fab);
    final ArrayList<String> resultArray = getIntent().getStringArrayListExtra("mfavlist");

    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) {
            resultArray.add(kine);
            }
    });


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (phy_law_toggel.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

favouritepage.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favouritepage);

    ArrayList<String> mfavlist = new ArrayList<String>();

    Intent intent = new Intent(Favouritepage.this,Favouritepage.class);
    intent.putExtra(" mfavlist ", mfavlist);
    startActivity(intent);
}

每当我点击vectordrawable图标时,应用程序崩溃并显示NullPointerException错误

这是我的崩溃日志

  

AndroidRuntime:FATAL EXCEPTION:主要                     处理:com.example.alpit.formula2,PID:2991                     java.lang.NullPointerException:尝试调用虚方法&#39; boolean java.util.ArrayList.add(java.lang.Object)&#39;在null对象引用上                         at com.example.alpit.formula2.Kinematics $ 2.onClick(Kinematics.java:71)                         在android.view.View.performClick(View.java:4802)                         在android.view.View $ PerformClick.run(View.java:20102)                         在android.os.Handler.handleCallback(Handler.java:810)                         在android.os.Handler.dispatchMessage(Handler.java:99)                         在android.os.Looper.loop(Looper.java:189)                         在android.app.ActivityThread.main(ActivityThread.java:5529)                         at java.lang.reflect.Method.invoke(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

1 个答案:

答案 0 :(得分:0)

夫妻俩:

  • 首先删除favoritepage.java中名称周围的额外空格
  • 同时使用putStringArrayListExtra代替putExtra

    intent.putStringArrayListExtra("mfavlist", mfavlist); 
    

编辑1

您可能还会考虑在创建Intent时实际指定其他活动:

Intent intent = new Intent(Favouritepage.this, Kinematics.class);