android构造函数未定义
我是android新手。 我得到了这个错误:构造函数GpsData(btnReport)未定义 对于这一行:android构造函数未定义,android,constructor,undefined,Android,Constructor,Undefined,我是android新手。 我得到了这个错误:构造函数GpsData(btnReport)未定义 对于这一行: gps = new GpsData(btnReport.this); 守则: public class btnReport extends AsyncTask<String, String, Void> { // GPSTracker class GpsData gps; @Override protected Void doInBackg
gps = new GpsData(btnReport.this);
守则:
public class btnReport extends AsyncTask<String, String, Void> {
// GPSTracker class
GpsData gps;
@Override
protected Void doInBackground(String... params) {
String artID = params[0];
// create class object
gps = new GpsData(btnReport.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mlat = String.valueOf(latitude);
String mlon = String.valueOf(longitude);
// \n is for new line
new PostData().execute(mlat, mlon, artID);
gps.stopUsingGPS();
return null;
}
}
好的,下面是我的应用程序的路径:
星触觉:
Intent intent = new Intent(StartActivity.this, ReportActivity.class);
startActivity(intent);
报告活动:
public class ReportActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
Button b = (Button) findViewById(R.id.report_TJ_btn1);
b.setOnClickListener(this);
b = (Button) findViewById(R.id.report_TJ_btn2);
b.setOnClickListener(this);
b = (Button) findViewById(R.id.report_TJ_btn3);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()== R.id.report_TJ_btn1) {
startActivity(new Intent(this,btnReport.class));
}
if(v.getId()== R.id.report_TJ_btn2) {
startActivity(new Intent(this,btnReport.class));
}
if(v.getId() == R.id.report_TJ_btn3) {
startActivity(new Intent(this,btnReport.class));
}
}
}
使用您的
activityname。此
(其中使用AsycTask
)而不是您的btnReport。此
public class btnReport extends AsyncTask<String, String, Void> {
// GPSTracker class
GpsData gps;
@Override
protected Void doInBackground(String... params) {
String artID = params[0];
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
gps = new GpsData(MainActivity.this);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mlat = String.valueOf(latitude);
String mlon = String.valueOf(longitude);
// \n is for new line
// new PostData().execute(mlat, mlon, artID);
gps.stopUsingGPS();
}
});
return null;
}
}
公共类btnReport扩展了异步任务{
//GPSTracker类
全球卫星定位系统;
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串artID=params[0];
MainActivity.this.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
gps=新的GpsData(MainActivity.this);
双纬度=gps.getLatitude();
double longitude=gps.getLongitude();
String mlat=String.valueOf(纬度);
String mlon=String.valueOf(经度);
//\n用于新行
//新建PostData().execute(mlat、mlon、artID);
停止使用gps();
}
});
返回null;
}
}
您需要向“GpsData”的构造函数传递什么样的数据类型
您正在向它传递一个AsyncTask
的实例,如果您想要一个上下文
(这种情况非常常见),您可以尝试:
gps = new GpsData(Application.getContext());
我只是认为这需要当前的环境。除非他发布了GpsData类,否则你是在胡乱猜测。现在在看到他的GpsData类之后,我认为你的方法是好的。猜得好@talhakosen当我尝试此操作时,我得到一个新错误:在中无法访问ReportActivity类型的封闭实例scope@talhakosen否,相同的错误:在范围中无法访问ReportActivity类型的封闭实例
gps = new GpsData(Application.getContext());