I just removed Butterknife from my Android app and replaced it with View binding.
This is the old code with Butterknife, which loads the data as expected:
public class PreloadActivity extends AppCompatActivity {
@SuppressLint("NonConstantResourceId")
@BindView(id.progressBar)
ProgressBar progressBar;
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preload);
Unbinder bind = ButterKnife.bind(this);
/* new LoadData().execute(); */
new LoadData().execute();
}
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("deprecation")
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
private class LoadData extends AsyncTask<Void, Integer, Void>{
DBHelper helper;
AppPreference preference;
double progress;
double maxprogress = 100;
public void onPreExecute() {
helper = new DBHelper(getApplicationContext());
preference = new AppPreference(getApplicationContext());
}
@Override
protected Void doInBackground(Void... voids) {
Boolean firstRun = preference.getFirstRun();
if (firstRun){
ArrayList<DBModel> combination1 = preLoadRaw(R.raw.comb1text);
ArrayList<DBModel> combination2 = preLoadRaw(R.raw.comb2text);
Log.d("length", String.valueOf(combination2.size()));
publishProgress((int) progress);
try{
helper.open();
} catch (SQLException e){
e.printStackTrace();
}
double progressMaxInsert = 100.0;
double progressDiff = (progressMaxInsert - progress) /
(combination1.size() + combination2.size());
helper.insertTransaction(combination1, true);
progress += progressDiff;
publishProgress((int) progress);
helper.insertTransaction(combination2, false);
progress += progressDiff;
publishProgress((int) progress);
helper.close();
preference.setFirstRun(false);
publishProgress((int) maxprogress);
}
else {
try{
synchronized (this){
this.wait(1000);
publishProgress(50);
this.wait(300);
publishProgress((int) maxprogress);
}
} catch (Exception ignored) {
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void aVoid) {
Intent i = new Intent(PreloadActivity.this, com.myapp.name.MainActivity.class);
startActivity(i);
finish();
}
}
private ArrayList<DBModel> preLoadRaw(int data) {
ArrayList<DBModel> list = new ArrayList<>();
BufferedReader reader;
try{
Resources res = getResources();
InputStream raw_DB = res.openRawResource(data);
reader = new BufferedReader(new InputStreamReader(raw_DB));
String line;
do{
line = reader.readLine();
String[] splitstr = line.split("t");
DBModel DB;
DB = new DNModel(splitstr[0], splitstr[1]);
list.add(DB);
} while (true);
}
catch (Exception e){
e.printStackTrace();
}
return list;
}
Now in the new code, the data doesn’t load anymore. Android Studio tells me that LoadData is never used. The command “execute” does not work. The app crashes. This construction apparently only works with Butterknife. How can I replace it? Any idea what I am doing wrong?
public class PreloadActivity extends AppCompatActivity {
@SuppressLint("NonConstantResourceId")
private ProgressBar progressBar;
//Private field 'progressBar' is never assigned
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preload);
com.myapp.name.databinding.ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
new LoadData().execute(); //execute does not work, app crashes
}
@SuppressLint("StaticFieldLeak")
@SuppressWarnings("deprecation")
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
private class LoadData extends AsyncTask<Void, Integer, Void>{
//Private inner class 'LoadData' is never used
DBHelper helper;
AppPreference preference;
double progress;
double maxprogress = 100;
public void onPreExecute() {
helper = new DBHelper(getApplicationContext());
preference = new AppPreference(getApplicationContext());
}
@Override
protected Void doInBackground(Void... voids) {
Boolean firstRun = preference.getFirstRun();
if (firstRun){
ArrayList<DBModel> combination1 = preLoadRaw(R.raw.comb1text);
ArrayList<DBModel> combination2 = preLoadRaw(R.raw.comb2text);
Log.d("length", String.valueOf(combination2.size()));
publishProgress((int) progress);
try{
helper.open();
} catch (SQLException e){
e.printStackTrace();
}
double progressMaxInsert = 100.0;
double progressDiff = (progressMaxInsert - progress) /
(combination1.size() + combination2.size());
helper.insertTransaction(combination1, true);
progress += progressDiff;
publishProgress((int) progress);
helper.insertTransaction(combination2, false);
progress += progressDiff;
publishProgress((int) progress);
helper.close();
preference.setFirstRun(false);
publishProgress((int) maxprogress);
}
else {
try{
synchronized (this){
this.wait(1000);
publishProgress(50);
this.wait(300);
publishProgress((int) maxprogress);
}
} catch (Exception ignored) {
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void aVoid) {
Intent i = new Intent(PreloadActivity.this, com.myapp.name.MainActivity.class);
startActivity(i);
finish();
}
}
public class LoadDataImpl extends LoadData { }
private ArrayList<DBModel> preLoadRaw(int data) {
ArrayList<DBModel> list = new ArrayList<>();
BufferedReader reader;
try{
Resources res = getResources();
InputStream raw_DB = res.openRawResource(data);
reader = new BufferedReader(new InputStreamReader(raw_DB));
String line;
do{
line = reader.readLine();
String[] splitstr = line.split("t");
DBModel DB;
DB = new DBModel(splitstr[0], splitstr[1]);
list.add(DB);
} while (true);
}
catch (Exception e){
e.printStackTrace();
}
return list;
}