将 activity 放入收藏夹页面时出错
Error while putting activity inside the favourite page
我有一个矢量可绘制图像。它的用途是,无论何时单击它,activity 都会被添加到收藏夹页面。
这是我的 kinematics.java class
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: main
Process: com.example.alpit.formula2, PID: 2991
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.onClick(Kinematics.java:71)
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)
两件事:
- 首先删除 favoritepage.java
中名称周围多余的白色 space
也用putStringArrayListExtra
代替putExtra
intent.putStringArrayListExtra("mfavlist", mfavlist);
编辑 1
您还可以考虑在创建 Intent
:
时实际指定 other activity
Intent intent = new Intent(Favouritepage.this, Kinematics.class);
我有一个矢量可绘制图像。它的用途是,无论何时单击它,activity 都会被添加到收藏夹页面。
这是我的 kinematics.java class
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: main Process: com.example.alpit.formula2, PID: 2991 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.onClick(Kinematics.java:71) 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)
两件事:
- 首先删除 favoritepage.java 中名称周围多余的白色 space
也用
putStringArrayListExtra
代替putExtra
intent.putStringArrayListExtra("mfavlist", mfavlist);
编辑 1
您还可以考虑在创建 Intent
:
Intent intent = new Intent(Favouritepage.this, Kinematics.class);