当我尝试将进度条的可见性设置为VISIBLE时,我得到一个NPE(NullPointerException),即使我在调用导致应用程序崩溃的AsyncTask之前已经初始化它。 奇怪的是,我有另一个AsyncTask做同样的事情但是有效,我想因为我已经在RecyclerView上实现了一个onClickListener。 这是主要活动Java代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView mTrainsRv;
private TrainAdapter mTrainAdapter;
ArrayList<Train> trains = new ArrayList<>();
ProgressBar mRvProgress;
ProgressBar mArrivalProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mArrivalProgress = findViewById(R.id.arrival_time_pb);
mRvProgress = findViewById(R.id.rv_progress_bar);
mTrainsRv = (RecyclerView) findViewById(R.id.train_rv);
mTrainsRv.setLayoutManager(new LinearLayoutManager(this));
mTrainAdapter = new TrainAdapter(createDefaultTrains());
mTrainsRv.setAdapter(mTrainAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
trains.add(new Train(4, "Berowra Platform 4", 12, "Late", "Beverly", "15:18"));
mTrainAdapter.notifyDataSetChanged();
}
});
mTrainAdapter.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
new RowUpdateAsync(position).execute();
Toast.makeText(MainActivity.this, "YAS", Toast.LENGTH_SHORT).show();
}
});
}
private List<Train> createDefaultTrains() {
trains.add(new Train(0, "Albion Park Platform 1", 3, "On Time", "Allawah", "14:11"));
trains.add(new Train(1, "Arncliffe Platform 2", 4, "Late", "Central", "14:34"));
trains.add(new Train(2, "Artarmion Platform 3", 7, "On Time", "Ashfield", "15:01"));
trains.add(new Train(3, "Berowra Platform 4", 12, "Late", "Beverly", "15:18"));
return trains;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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.
// Handle item selection
switch (item.getItemId()) {
case R.id.action_delete:
trains.clear();
mTrainAdapter.notifyDataSetChanged();
return true;
case R.id.action_refresh:
new ViewUpdateAsync().execute();
return true;
case R.id.action_quit:
Toast.makeText(this, R.string.quit_menu,
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class ViewUpdateAsync extends AsyncTask<Void, Void, Integer[]> {
@Override
protected Integer[] doInBackground(Void... voids) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random r = new Random();
Integer[] arr = new Integer[trains.size()];
for (int i = 0; i < trains.size(); i++) {
int min = 1;
int max = 20;
int ran = r.nextInt(max - min + 1) + min;
arr[i] = ran;
}
return arr;
}
@Override
protected void onPreExecute() {
mRvProgress.setVisibility(View.VISIBLE);
mTrainsRv.setAlpha(0);
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer[] arrayList) {
super.onPostExecute(arrayList);
for (int i = 0; i < trains.size(); i++) {
trains.get(i).setArrivalTime(arrayList[i]);
}
mTrainAdapter.notifyDataSetChanged();
mTrainsRv.setAlpha(1);
mRvProgress.setVisibility(View.GONE);
}
}
private class RowUpdateAsync extends AsyncTask<Void, Void, Integer> {
private int mPosition;
private RowUpdateAsync(int position) {
this.mPosition = position;
}
@Override
protected Integer doInBackground(Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random r = new Random();
int min = 1;
int max = 20;
int ran = r.nextInt(max - min + 1) + min;
return ran;
}
@Override
protected void onPreExecute() {
mArrivalProgress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
trains.get(mPosition).setArrivalTime(integer);
mTrainAdapter.notifyDataSetChanged();
mArrivalProgress.setVisibility(View.GONE);
}
}
要获得进一步的帮助,这是Adapter类的代码:
public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.ViewHolder> {
private List<Train> mTrains;
public ItemClickListener mClickListener;
public TrainAdapter(List<Train> trains) {
mTrains = trains;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.train_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Train train = mTrains.get(position);
holder.mArrivalTimeTv.setText(String.valueOf(train.getArrivalTime()));
holder.mPlatformTv.setText(train.getPlatform());
holder.mStatusTv.setText(train.getStatus());
holder.mDestinationTimeTv.setText(train.getDestinationTime());
holder.mDestinationTv.setText(train.getDestination());
}
@Override
public int getItemCount() {
return mTrains.size();
}
public void setClickListener(ItemClickListener itemClickListener) {
mClickListener = itemClickListener;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mArrivalTimeTv, mPlatformTv, mStatusTv, mDestinationTimeTv, mDestinationTv;
public LinearLayout mArrivalLayout;
public ViewHolder(View view) {
super(view);
mArrivalTimeTv = (TextView) view.findViewById(R.id.arrival_time);
mPlatformTv = (TextView) view.findViewById(R.id.platform);
mStatusTv = (TextView) view.findViewById(R.id.status);
mDestinationTimeTv = (TextView) view.findViewById(R.id.destination_time);
mDestinationTv = (TextView) view.findViewById(R.id.destination);
mArrivalLayout = view.findViewById(R.id.time_layout);
mArrivalLayout.setOnClickListener(this);
}
@Override
public void onClick (View v){
if (mClickListener != null) {
mClickListener.onClick(v, getAdapterPosition());
}
}
}
崩溃日志:
04-28 16:51:56.064 4880-4880/com.mad.exercise5 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mad.exercise5, PID: 4880
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.mad.exercise5.MainActivity$RowUpdateAsync.onPreExecute(MainActivity.java:161)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:648)
at android.os.AsyncTask.execute(AsyncTask.java:595)
at com.mad.exercise5.MainActivity$2.onClick(MainActivity.java:55)
at com.mad.exercise5.TrainAdapter$ViewHolder.onClick(TrainAdapter.java:69)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:1)
由于它的空指针在使用之前尝试再次初始化asyntask中的进度条