eclipse 令牌“类”上的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14046525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error on token "class"
提问by Ameya Savale
I am trying to create a nested class in order to use the AsyncTask, however eclipse gives me an error on the class SendData saying "Syntax error on token "class", invalid type" why is it giving me that error?
我正在尝试创建一个嵌套类以使用 AsyncTask,但是 eclipse 在类 SendData 上给了我一个错误,说“令牌“类”上的语法错误,无效类型”为什么它给了我那个错误?
package com.example.myfirstapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
String url = "http://www.mySIte.com/phpFIle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
new SendData().execute();
}
private class SendData() extends AsyncTask<Void, Void, Void>{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try{
List<NameValuePair> nameValues = new ArrayList<NameValuePair>(1);
nameValues.add(new BasicNameValuePair("id", "12345"));
httppost.setEntity(new UrlEncodedFormEntity(nameValues));
System.out.println("Before response");
HttpResponse response = httpclient.execute(httppost);
System.out.println("After response");
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
回答by PermGenError
sendData() is a method syntax, not a class
sendData() 是一种方法语法,而不是一个类
private class SendData() extends AsyncTask<Void, Void, Void>{
should be
应该
private class SendData extends AsyncTask<Void, Void, Void>{
回答by kosa
private class SendData()
You have both method
and class
syntax here. Not sure which one you want.
你在这里有method
和class
语法。不确定你想要哪一个。
If you want to define a class then remove ()
如果你想定义一个类然后删除 ()