我从sql server检索数据时创建了一个异步任务 像这样
private class MyAsyncTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
// Runs on the UI thread before doInBackground
// Good for toggling visibility of a progress indicator
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected String doInBackground(String... strings) {
String someBitmap ="";
String menuListSTR = "";
if (MenuActivity.Prst_ID.trim() == "-1")
menuListSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
else
menuListSTR = "select dbo.MenuItems.Item_ID, dbo.Items.Code, dbo.Items.Name, dbo.Items.Name2, dbo.Items.PrintOnChick, dbo.Items.Taxable, dbo.Items.NoServiceCharge, dbo.Items.PrintOnReport,Case { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), 0) } when 0 then dbo.Items.StaticPrice Else { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), dbo.Items.StaticPrice) } END AS Price From dbo.MenuItems LEFT OUTER JOIN dbo.Items ON dbo.MenuItems.Item_ID = dbo.Items.ID Where (dbo.MenuItems.Preset_ID = " + MenuActivity.Prst_ID + ") AND (dbo.MenuItems.Rest_ID_Active = " + ConnectionClass.Rest_ID + ") AND (dbo.MenuItems.OutLet_ID_Active = " + ConnectionClass.OutletID + ") AND (dbo.Items.Active = 1) ORDER BY dbo.MenuItems.SortNumber";
ResultSet rs = ConnectionClass.Ret_RS(menuListSTR);
try {
while (rs.next()) {
SortedMap<String, String> sm = new TreeMap<String, String>();
sm.put("Item_ID", rs.getString("Item_ID"));
sm.put("Name", rs.getString("Name"));
sm.put("Price", rs.getString("Price"));
sm.put("PrintOnChick", rs.getString("PrintOnChick"));
sm.put("Taxable", rs.getString("Taxable"));
sm.put("NoServiceCharge", rs.getString("NoServiceCharge"));
sm.put("PrintOnReport", rs.getString("PrintOnReport"));
if (TablesFragment.Check_Items.containsKey(rs.getString("Item_ID"))) {
SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(rs.getString("Item_ID"));
sm.put("Qty", sm1.get("Qty"));
} else
sm.put("Qty", "0");
alphabets.add(sm);
listMenuArray.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return someBitmap;
}
// protected void onProgressUpdate(Progress... values) {
// // Executes whenever publishProgress is called from doInBackground
// // Used to update the progress indicator
// progressBar.setProgress(values[0]);
// }
protected void onPostExecute(Bitmap result) {
// This method is executed in the UIThread
// with access to the result of the long running task
menuListView.setAdapter(new CustomMenuLVAdapter(this, listMenuArray, alphabets));
// Hide the progress bar
progressBar.setVisibility(ProgressBar.INVISIBLE);
}
}
我收到了像这样的Underlined错误 这是我在其上放置异步任务方法的片段
package abtech.waiteriano.com.waitrer.fragments;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;
import abtech.waiteriano.com.waitrer.MenuActivity;
import abtech.waiteriano.com.waitrer.R;
import abtech.waiteriano.com.waitrer.adapters.CustomMenuLVAdapter;
import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
public class LVMenuFragment extends android.app.Fragment {
View rootView;
ListView menuListView;
public static ArrayList<String> listMenuArray = new ArrayList<String>();
ArrayList<SortedMap> alphabets = new ArrayList<SortedMap>();
public LVMenuFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_lvmenu, container, false);
menuListView = (ListView) rootView.findViewById(R.id.listView);
listMenuArray.clear();
alphabets.clear();
String menuListSTR = "";
if (MenuActivity.Prst_ID.trim() == "-1")
menuListSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
else
menuListSTR = "select dbo.MenuItems.Item_ID, dbo.Items.Code, dbo.Items.Name, dbo.Items.Name2, dbo.Items.PrintOnChick, dbo.Items.Taxable, dbo.Items.NoServiceCharge, dbo.Items.PrintOnReport,Case { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), 0) } when 0 then dbo.Items.StaticPrice Else { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), dbo.Items.StaticPrice) } END AS Price From dbo.MenuItems LEFT OUTER JOIN dbo.Items ON dbo.MenuItems.Item_ID = dbo.Items.ID Where (dbo.MenuItems.Preset_ID = " + MenuActivity.Prst_ID + ") AND (dbo.MenuItems.Rest_ID_Active = " + ConnectionClass.Rest_ID + ") AND (dbo.MenuItems.OutLet_ID_Active = " + ConnectionClass.OutletID + ") AND (dbo.Items.Active = 1) ORDER BY dbo.MenuItems.SortNumber";
ResultSet rs = ConnectionClass.Ret_RS(menuListSTR);
try {
while (rs.next()) {
SortedMap<String, String> sm = new TreeMap<String, String>();
sm.put("Item_ID", rs.getString("Item_ID"));
sm.put("Name", rs.getString("Name"));
sm.put("Price", rs.getString("Price"));
sm.put("PrintOnChick", rs.getString("PrintOnChick"));
sm.put("Taxable", rs.getString("Taxable"));
sm.put("NoServiceCharge", rs.getString("NoServiceCharge"));
sm.put("PrintOnReport", rs.getString("PrintOnReport"));
if (TablesFragment.Check_Items.containsKey(rs.getString("Item_ID"))) {
SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(rs.getString("Item_ID"));
sm.put("Qty", sm1.get("Qty"));
} else
sm.put("Qty", "0");
alphabets.add(sm);
listMenuArray.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
menuListView.setAdapter(new CustomMenuLVAdapter(this, listMenuArray, alphabets));
new MyAsyncTask().execute();
return rootView;
}
ProgressBar progressBar;
// The types specified here are the input data type, the progress type, and the result type
private class MyAsyncTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
// Runs on the UI thread before doInBackground
// Good for toggling visibility of a progress indicator
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected String doInBackground(String... strings) {
String someBitmap ="";
String menuListSTR = "";
if (MenuActivity.Prst_ID.trim() == "-1")
menuListSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
else
menuListSTR = "select dbo.MenuItems.Item_ID, dbo.Items.Code, dbo.Items.Name, dbo.Items.Name2, dbo.Items.PrintOnChick, dbo.Items.Taxable, dbo.Items.NoServiceCharge, dbo.Items.PrintOnReport,Case { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), 0) } when 0 then dbo.Items.StaticPrice Else { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), dbo.Items.StaticPrice) } END AS Price From dbo.MenuItems LEFT OUTER JOIN dbo.Items ON dbo.MenuItems.Item_ID = dbo.Items.ID Where (dbo.MenuItems.Preset_ID = " + MenuActivity.Prst_ID + ") AND (dbo.MenuItems.Rest_ID_Active = " + ConnectionClass.Rest_ID + ") AND (dbo.MenuItems.OutLet_ID_Active = " + ConnectionClass.OutletID + ") AND (dbo.Items.Active = 1) ORDER BY dbo.MenuItems.SortNumber";
ResultSet rs = ConnectionClass.Ret_RS(menuListSTR);
try {
while (rs.next()) {
SortedMap<String, String> sm = new TreeMap<String, String>();
sm.put("Item_ID", rs.getString("Item_ID"));
sm.put("Name", rs.getString("Name"));
sm.put("Price", rs.getString("Price"));
sm.put("PrintOnChick", rs.getString("PrintOnChick"));
sm.put("Taxable", rs.getString("Taxable"));
sm.put("NoServiceCharge", rs.getString("NoServiceCharge"));
sm.put("PrintOnReport", rs.getString("PrintOnReport"));
if (TablesFragment.Check_Items.containsKey(rs.getString("Item_ID"))) {
SortedMap<String, String> sm1 = TablesFragment.Check_Items.get(rs.getString("Item_ID"));
sm.put("Qty", sm1.get("Qty"));
} else
sm.put("Qty", "0");
alphabets.add(sm);
listMenuArray.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return someBitmap;
}
// protected void onProgressUpdate(Progress... values) {
// // Executes whenever publishProgress is called from doInBackground
// // Used to update the progress indicator
// progressBar.setProgress(values[0]);
// }
protected void onPostExecute(Bitmap result) {
// This method is executed in the UIThread
// with access to the result of the long running task
menuListView.setAdapter(new CustomMenuLVAdapter(this, listMenuArray, alphabets));
// Hide the progress bar
progressBar.setVisibility(ProgressBar.INVISIBLE);
}
}
}
这是我的Adapter类
package abtech.waiteriano.com.waitrer.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.SortedMap;
import abtech.waiteriano.com.waitrer.R;
import abtech.waiteriano.com.waitrer.fragments.LVMenuFragment;
import abtech.waiteriano.com.waitrer.fragments.TablesFragment;
/**
* Created by dell on 3/7/2017.
*/
public class CustomMenuLVAdapter extends BaseAdapter {
ArrayList<String> resultMenuItems;
ArrayList<SortedMap> Items_Details;
LVItemHolder lvItemHolder;
Context context;
private static LayoutInflater inflater = null;
public CustomMenuLVAdapter(LVMenuFragment lvMenuFragment, ArrayList<String> listMenuArray, ArrayList<SortedMap> alphabets) {
resultMenuItems = listMenuArray;
Items_Details = alphabets;
context = lvMenuFragment.getActivity();
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return resultMenuItems.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class LVItemHolder {
TextView listMenuTV, txtQTY;
RelativeLayout qtyView;
Button minusBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LVItemHolder lvItemHolder = new LVItemHolder();
View rowView;
rowView = inflater.inflate(R.layout.menu_list_item, null);
lvItemHolder.listMenuTV = (TextView) rowView.findViewById(R.id.menulistTV2);
lvItemHolder.txtQTY = (TextView) rowView.findViewById(R.id.txtQTY);
lvItemHolder.minusBtn = (Button) rowView.findViewById(R.id.minusBtn);
lvItemHolder.listMenuTV.setText(resultMenuItems.get(position));
SortedMap<String, String> sm = Items_Details.get(position);
lvItemHolder.txtQTY.setText(sm.get("Qty"));
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int q = 0;
q = Integer.valueOf(lvItemHolder.txtQTY.getText().toString());
q++;
lvItemHolder.txtQTY.setText("" + q);
SortedMap<String, String> sm = Items_Details.get(position);
sm.put("Qty", "" + q);
TablesFragment.Check_Items.put(sm.get("Item_ID"), sm);
// Toast.makeText(context, "You Clicked "+resultMenuItems.get(position), Toast.LENGTH_LONG).show();
}
});
lvItemHolder.minusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int q = 0;
q = Integer.valueOf(lvItemHolder.txtQTY.getText().toString());
if (q > 0)
q--;
lvItemHolder.txtQTY.setText("" + q);
SortedMap<String, String> sm = Items_Details.get(position);
sm.put("Qty", "" + q);
TablesFragment.Check_Items.put(sm.get("Item_ID"), sm);
}
});
return rowView;
}
}
android监视器错误
03-22 17:20:12.515 29962-30011/abtech.waiteriano.com.waitrer E/GED: Failed to get GED Log Buf, err(0)
03-22 17:20:12.531 29962-30011/abtech.waiteriano.com.waitrer E/[PropSet]: connect fail
03-22 17:20:12.531 29962-30011/abtech.waiteriano.com.waitrer E/libc: __system_property_set error : retry fail, errno 13(Permission denied)
03-22 17:20:12.531 29962-30011/abtech.waiteriano.com.waitrer E/[PropSet]: send_prop_msg return err -5
03-22 17:20:17.485 29962-29962/abtech.waiteriano.com.waitrer E/AndroidRuntime: FATAL EXCEPTION: main
Process: abtech.waiteriano.com.waitrer, PID: 29962
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at abtech.waiteriano.com.waitrer.fragments.LVMenuFragment$MyAsyncTask.onPreExecute(LVMenuFragment.java:85)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
at android.os.AsyncTask.execute(AsyncTask.java:539)
at abtech.waiteriano.com.waitrer.fragments.LVMenuFragment.onCreateView(LVMenuFragment.java:76)
at android.app.Fragment.performCreateView(Fragment.java:2069)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
at android.app.BackStackRecord.run(BackStackRecord.java:852)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6102)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
答案 0 :(得分:2)
使用LVMenuFragment.this
,因为只使用this
会引用AsyncTask
类。您想要传递LVMenuFragment
对象,而不是AsyncTask
。
答案 1 :(得分:1)
尝试在空对象引用上调用虚方法'void android.widget.ProgressBar.setVisibility(int)'
progressBar
未初始化,请在progressBar = (ProgressBar) rootView.findViewById(R.id.your_progress_bar_id);
方法中调用onCreateView
修改强>
AsyncTask
方法有误:onPostExecute
应该有String
参数,这导致onPostExecute
从未被调用过。 AsyncTask应该如下所示:
private class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
...
}
@Override
protected String doInBackground(String... strings) {
String someBitmap;
...
return someBitmap;
}
@Override
protected void onPostExecute(String /*instead Bitmap*/ result) {
...
}
}
使用 @Override
注释来避免此类错误。