Main activity
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.app.ActionBar;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.navigation.NavigationView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URL;
import eightbitlab.com.blurview.BlurView;
import eightbitlab.com.blurview.RenderScriptBlur;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public Toolbar toolbar;
BlurView blurView, blurView1, blurView2, blurView3;
public DrawerLayout drawerLayout;
boolean doubleBackToExitPressedOnce = false;
public NavController navController;
public NavigationView navigationView;
TextView tv, tv1, tv2, tv3;
ActionBarDrawerToggle actionBarDrawerToggle;
int i = 0;
CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
// getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
blurView = findViewById(R.id.blurView);
blurView1 = findViewById(R.id.blurView1);
blurView2 = findViewById(R.id.blurView2);
//blurView3 = findViewById(R.id.blurView3);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
try {
blurBackground();
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
tv = (TextView) findViewById(R.id.text);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = findViewById(R.id.text3);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
// setupNavigation();
// Button b = findViewById(R.id.button);
new doit().execute();
} catch (Exception e) {
e.printStackTrace();
}
final SwipeRefreshLayout pullToRefresh = findViewById(R.id.refresh);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// new doit().execute();
// pullToRefresh.setRefreshing(false);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
new doit().execute();
pullToRefresh.setRefreshing(false);
}
}, 3000);
}
});
//Timer code
timer = new CountDownTimer(20000, 20) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
try {
// yourMethod();
new doit().execute();
} catch (Exception e) {
Log.e("Error", "Error: " + e.toString());
}
}
}.start();
// b.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// new doit().execute();
// // tv.setText("hellobro");
// }
// });
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.first:
// String url = "http://www.youtube.com";
drawerLayout.closeDrawers();
// Intent i = new Intent(Intent.ACTION_VIEW);
// i.setData(Uri.parse(url));
// startActivity(i);
loadWebpage();
//Toast.makeText(this,"yghgheiufheief",Toast.LENGTH_LONG).show();
return true;
case R.id.second:
drawerLayout.closeDrawers();
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public class doit extends AsyncTask < Void, Void, Void > {
String word,
word1,
word2;
@Override
protected Void doInBackground(Void...params) {
try {
Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/").get();
// URL url = new URL("https://www.worldometers.info/coronavirus/");
// Document doc = Jsoup.parse(url, 3000);
// Elements parents = doc.select("div.main_stats");
// for (Element child : parents) {
// Element label = child.select("h3.product_title").first();
// Element score = child.select("span.metascore_w.medium.game").first();
// System.out.println("Game **" + label.text()+ "** has a Metascore of ->> " + score.text());
// word = doc.text();
Element coronaCases = doc.select("div[class=maincounter-number]").get(0);
Element deaths = doc.select("div[class=maincounter-number]").get(1);
Element recovered = doc.select("div[class=maincounter-number]").get(2);
word = coronaCases.text();
word1 = deaths.text();
word2 = recovered.text();
timer.start();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
tv.setText(word);
tv1.setText(word1);
tv2.setText(word2);
}
}
// public void yourMethod(){
//
// i++;
// tv3.setText(String.valueOf(i));
// timer.start();
// }
//private void setupNavigation() {
//
// toolbar = findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
//
// drawerLayout = findViewById(R.id.drawer_layout);
//
// navigationView = findViewById(R.id.navigationView);
//
// navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//
// NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
//
// NavigationUI.setupWithNavController(navigationView, navController);
//
// navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
//
//}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
public void blurBackground() {
try {
float radius = 10 f;
View decorView = getWindow().getDecorView();
//ViewGroup you want to start blur from. Choose root as close to BlurView in hierarchy as possible.
ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content);
//Set drawable to draw in the beginning of each blurred frame (Optional).
//Can be used in case your layout has a lot of transparent space and your content
//gets kinda lost after after blur is applied.
Drawable windowBackground = decorView.getBackground();
blurView.setupWith(rootView)
.setFrameClearDrawable(windowBackground)
.setBlurAlgorithm(new RenderScriptBlur(this))
.setBlurRadius(radius)
.setHasFixedTransformationMatrix(true);
blurView1.setupWith(rootView)
.setFrameClearDrawable(windowBackground)
.setBlurAlgorithm(new RenderScriptBlur(this))
.setBlurRadius(radius)
.setHasFixedTransformationMatrix(true);
blurView2.setupWith(rootView)
.setFrameClearDrawable(windowBackground)
.setBlurAlgorithm(new RenderScriptBlur(this))
.setBlurRadius(radius)
.setHasFixedTransformationMatrix(true);
// blurView3.setupWith(rootView)
// .setFrameClearDrawable(windowBackground)
// .setBlurAlgorithm(new RenderScriptBlur(this))
// .setBlurRadius(radius)
// .setHasFixedTransformationMatrix(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadWebpage() {
goToUrl("https://www.youtube.com/channel/UCSzX1LdNJ4bxF19enoq1l3w?view_as=subscriber");
}
private void goToUrl(String url) {
Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra("url", url);
startActivity(intent);
}
@Override
public void onBackPressed() {
if (this.drawerLayout.isDrawerOpen(GravityCompat.START)) {
this.drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
My activity_main.xml
我认为主要问题在我的xml文件中 我的应用程序运行良好,但是当我滚动应用程序时它崩溃了,我不知道这是什么问题,我唯一能说的是即使我在代码中没有使用任何数组列表,它也会引发outoutboundexception错误请.... 我正在使用Augustbitlab库使用模糊布局,我不知道这是否会让我的应用崩溃
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearlayout_root_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<include layout="@layout/layout"/>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
style="@style/DrawerArrowStyle"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/aa" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COVID-19"
android:textSize="70dp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="70dp"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total Cases Worldwide"
android:textSize="40dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:blurOverlayColor="@color/colorOverlay">
<!--Any child View here, TabLayout for example-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#980CF9"
android:fontFamily="sans-serif-light"
android:id="@+id/text"/>
</eightbitlab.com.blurview.BlurView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Deaths:"
android:textSize="40dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:blurOverlayColor="@color/colorOverlay">
<!--Any child View here, TabLayout for example-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="55dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#F53C17"
android:fontFamily="sans-serif-light"
android:id="@+id/text1"/>
</eightbitlab.com.blurview.BlurView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recovered"
android:textSize="30dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<eightbitlab.com.blurview.BlurView
android:id="@+id/blurView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:blurOverlayColor="@color/colorOverlay">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#2FF517"
android:fontFamily="sans-serif-light"
android:id="@+id/text2"/>
</eightbitlab.com.blurview.BlurView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#2FF517"
android:fontFamily="sans-serif-light"
android:id="@+id/text3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recovered"
android:textSize="60dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recovered"
android:textSize="60dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recovered"
android:textSize="60dp"
android:gravity="center"
android:fontFamily="sans-serif-light"
android:textAllCaps="false"
android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/heaader_layout"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>