我的android webview片段应用程序崩溃了,在我添加了一个按钮从一个片段转到另一个片段后,控制台上的错误是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ahmed.stories, PID: 11846
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ahmed.stories.MainActivity.setUrl(java.lang.String)' on a null object reference at com.ahmed.stories.Saying$3.onPageStarted(Saying.java:127)
at com.android.webview.chromium.WebViewContentsClientAdapter.onPageStarted(WebViewContentsClientAdapter.java:517)
at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:144)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
它说问题是第127行与((MainActivity) getActivity()).setUrl(url);
有关,但是我发现它没有任何问题,我想到了android并试图学习,也许你们可以提供帮助。
private void initWebView() {
home_web.setWebChromeClient(new Saying.MyWebChromeClient(getActivity()));
home_web.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
((MainActivity) getActivity()).setUrl(url);
progressBar.setVisibility(View.VISIBLE);
// invalidateOptionsMenu();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
home_web.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
// invalidateOptionsMenu();
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
// invalidateOptionsMenu();
}
});
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
TextView home_tv, stories, bookmarks, drafts, fakecheck, lost, leaving;
CardView lyt_top_bar;
View toolbar_line;
BottomNavigationView navigation;
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Home home = new Home();
fragmentTransaction.replace(R.id.fragmentContainer, home, "Home");
fragmentTransaction.commit();
return true;
case R.id.navigation_search:
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Search search = new Search();
fragmentTransaction.replace(R.id.fragmentContainer, search, "Home");
fragmentTransaction.commit();
return true;
case R.id.navigation_edit:
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Edit edit = new Edit();
fragmentTransaction.replace(R.id.fragmentContainer, edit, "Home");
fragmentTransaction.commit();
return true;
case R.id.navigation_message:
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Notification notification = new Notification();
fragmentTransaction.replace(R.id.fragmentContainer, notification, "Home");
fragmentTransaction.commit();
return true;
case R.id.navigation_profile:
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Profile profile = new Profile();
fragmentTransaction.replace(R.id.fragmentContainer, profile, "Home");
fragmentTransaction.commit();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.navigation));
bookmarks = findViewById(R.id.bookmarks);
stories = findViewById(R.id.stories);
home_tv = findViewById(R.id.home_tv);
lyt_top_bar = findViewById(R.id.lyt_top_bar);
toolbar_line = findViewById(R.id.toolbar_line);
stories.setOnClickListener(this);
home_tv.setOnClickListener(this);
bookmarks.setOnClickListener(this);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Home home = new Home();
fragmentTransaction.replace(R.id.fragmentContainer, home, "Home");
fragmentTransaction.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main3, 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) {
shareTextUrl();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
showSystemUI();
if (id == R.id.nav_camera) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Profile profile = new Profile();
fragmentTransaction.replace(R.id.fragmentContainer, profile, "Home");
fragmentTransaction.commit();
// Handle the camera action
} else if (id == R.id.nav_gallery) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Edit edit = new Edit();
fragmentTransaction.replace(R.id.fragmentContainer, edit, "Home");
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Drafts drafts = new Drafts();
fragmentTransaction.replace(R.id.fragmentContainer, drafts, "Home");
fragmentTransaction.commit();
} else if (id == R.id.nav_manage) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Fakecheck fakecheck = new Fakecheck();
fragmentTransaction.replace(R.id.fragmentContainer, fakecheck, "Home");
fragmentTransaction.commit();
} else if (id == R.id.nav_lost) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Lost lost = new Lost();
fragmentTransaction.replace(R.id.fragmentContainer, lost, "Home");
fragmentTransaction.commit();
} else if (id == R.id.nav_send) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Leaving leaving = new Leaving();
fragmentTransaction.replace(R.id.fragmentContainer, leaving, "Home");
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void hideSystemUI() {
lyt_top_bar.setVisibility(View.GONE);
navigation.setVisibility(View.GONE);
toolbar_line.setVisibility(View.GONE);
}
public void showSystemUI() {
lyt_top_bar.setVisibility(View.VISIBLE);
navigation.setVisibility(View.VISIBLE);
toolbar_line.setVisibility(View.VISIBLE);
// View decorView = getWindow().getDecorView();
// decorView.setSystemUiVisibility(
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// );
}
@Override
public void onClick(View v) {
showSystemUI();
if (v == home_tv) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Saying saying = new Saying();
fragmentTransaction.replace(R.id.fragmentContainer, saying, "Home");
fragmentTransaction.commit();
saying.hideProgress();
hideSystemUI();
}
if (v == bookmarks) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Bookmarks bookmarks = new Bookmarks();
fragmentTransaction.replace(R.id.fragmentContainer, bookmarks, "Home");
fragmentTransaction.commit();
}
if (v == drafts) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Drafts drafts = new Drafts();
fragmentTransaction.replace(R.id.fragmentContainer, drafts, "Home");
fragmentTransaction.commit();
}
if (v == fakecheck) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Fakecheck fakecheck = new Fakecheck();
fragmentTransaction.replace(R.id.fragmentContainer, fakecheck, "Home");
fragmentTransaction.commit();
}
if (v == lost) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Lost lost = new Lost();
fragmentTransaction.replace(R.id.fragmentContainer, lost, "Home");
fragmentTransaction.commit();
}
if (v == leaving) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Leaving leaving = new Leaving();
fragmentTransaction.replace(R.id.fragmentContainer, leaving, "Home");
fragmentTransaction.commit();
}
if (v == stories) {
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Trending trending = new Trending();
fragmentTransaction.replace(R.id.fragmentContainer, trending, "Home");
fragmentTransaction.commit();
}
}
String urlOpen="";
public void setUrl(String url){
this.urlOpen = url;
}
public void shareTextUrl(){
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, urlOpen);
startActivity(Intent.createChooser(share, "We Only share with the people we care!"));
}
public void goToHome(){
showSystemUI();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
Home home = new Home();
fragmentTransaction.replace(R.id.fragmentContainer, home, "Home");
fragmentTransaction.commit();
}
}