2017-02-24 47 views
2

我有一個矢量可繪製圖像。它的用處在於,無論何時點擊該活動將被添加到favouritepage頁面。將活動放入喜歡頁面時出錯

這裏是我的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錯誤

這裏是我的crashlog

AndroidRuntime:致命例外:main 進程:com.example.alpit.formula2,PID:2991 java.lang.NullPointerException:試圖調用虛方法'boolean java.util.ArrayList.add(java.lang.Object) '上的空對象引用 在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) 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)(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit的.java:950) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

回答

0

兩件事情:

  • 首先去掉周圍的名額外的空格in favoritepage.java
  • 也可用putStringArrayListExtra而不是putExtra

    intent.putStringArrayListExtra("mfavlist", mfavlist); 
    

編輯1

您也可以考慮到實際指定其他活動時,您創建Intent

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

感謝您的幫助。它真的有幫助。活動被添加到最喜愛的列表,但也是我的應用程序他和同樣的錯誤不斷來臨 –

+0

@arunodaysingh它在哪一行崩潰? – bc004346

+0

@arunodaysingh Kinematics.java中這一行的目的是什麼:'final ArrayList mfavlist = new ArrayList ();'? – bc004346