【问题标题】:'' Can't resolve symbol '' Error in asyncTask code in Android Studio'' 无法解析符号'' Android Studio 中 asyncTask 代码中的错误
【发布时间】:2017-10-03 05:35:02
【问题描述】:

我正在尝试将用户在我的应用程序页面中输入的值发布到我在 000webhost 服务器中创建的数据库中。但它在 Android Studio 中显示了一些错误。

这是我的 java 文件,

IndividualUserFragment.java:

package fragmentsstartup.hh.safmical;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;

import javax.net.ssl.HttpsURLConnection;

import safmical.h.R;




/**
 * Created by hadvani on 4/16/2017.
 */

public class IndividualUserFragment extends Fragment {
    EditText name;
    EditText adhar;
    EditText email;
    EditText password;
    EditText contact;
    Button bt;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View rootview= inflater.inflate(R.layout.fragment_individualuser,container,false);
        bt = (Button) rootview.findViewById(R.id.button2);
        name= (EditText) rootview.findViewById(R.id.editText2);
        adhar= (EditText) rootview.findViewById(R.id.editText3);
        email= (EditText) rootview.findViewById(R.id.editText4);
        password= (EditText) rootview.findViewById(R.id.editText5);
        contact= (EditText) rootview.findViewById(R.id.editText6);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                AsyncTask obj = new AsyncDemo();
//                obj.execute();
                new myasyncDemo().execute();

            }
        });



        return rootview;
    }
}
class myasyncDemo extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {

        try {

            URL url = new URL("***************"); // here is my URL path

            JSONObject postDataParams = new JSONObject();
            postDataParams.put("name", name.getText().toString());//here it shows suggestion on name field that "can not resolve symbol name"
            postDataParams.put("adhar", adhar.getText().toString());//here also same "can not resolve symbol adhar"
            postDataParams.put("email", email.getText().toString());//here also same "can not resolve symbol email "

            postDataParams.put("password", password.getText().toString());//here also same "can not resolve symbol password "
            postDataParams.put("contact", contact.getText().toString());//here also same "can not resolve symbol contact"

            Log.e("params", postDataParams.toString());

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();

            int responseCode = conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                                conn.getInputStream()));
                StringBuffer sb = new StringBuffer("");
                String line = "";

                while ((line = in.readLine()) != null) {

                    sb.append(line);
                    break;
                }

                in.close();
                return sb.toString();

            } else {
                return new String("false : " + responseCode);
            }
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


    public String getPostDataString(JSONObject params) throws Exception {

        StringBuilder result = new StringBuilder();
        boolean first = true;

        Iterator<String> itr = params.keys();

        while (itr.hasNext()) {

            String key = itr.next();
            Object value = params.get(key);

            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(key, "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(value.toString(), "UTF-8"));

        }
        return result.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.e("DATA", result);

    }
}
}

我的 xml 文件:

fragment_individualuser.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#e6e6fa">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/button2"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="14dp"
                android:text="save"
                android:textSize="20dp"
                android:onClick="save"
                android:layout_below="@+id/editText6"
                android:layout_alignLeft="@+id/editText6"
                android:layout_alignStart="@+id/editText6" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Contact"
                android:layout_marginTop="20dp"
                android:layout_below="@+id/editText5" android:background="#ffffff"
                android:layout_alignLeft="@+id/editText5"
                android:layout_alignStart="@+id/editText5"
                android:layout_alignRight="@+id/editText4"
                android:layout_alignEnd="@+id/editText4" />

            <EditText
                android:id="@+id/editText5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" android:background="#ffffff"
                android:inputType="textPersonName"
                android:layout_marginTop="20dp"
                android:hint="Create Password(6 to 10 characters)"
                android:layout_below="@+id/editText4"
                android:layout_alignLeft="@+id/editText4"
                android:layout_alignStart="@+id/editText4"
                android:layout_alignRight="@+id/editText4"
                android:layout_alignEnd="@+id/editText4" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Email Address" android:background="#ffffff"
                android:layout_marginTop="20dp"
                android:layout_below="@+id/editText3"
                android:layout_alignLeft="@+id/editText3"
                android:layout_alignStart="@+id/editText3"
                android:layout_alignRight="@+id/textView2"
                android:layout_alignEnd="@+id/textView2" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName" android:background="#ffffff"
                android:hint="Adhar Number"
                android:layout_marginTop="20dp"
                android:layout_below="@+id/editText2"
                android:layout_alignLeft="@+id/editText2"
                android:layout_alignStart="@+id/editText2"
                android:layout_alignRight="@+id/textView2"
                android:layout_alignEnd="@+id/textView2" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="You are registering as a individual user."
                android:textColor="#000"
                android:textSize="25dp"
                android:layout_below="@+id/textView2"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Create Account"
                android:textColor="#000"
                android:textSize="45dp" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView3"
                android:layout_marginLeft="19dp"
                android:layout_marginStart="19dp"
                android:layout_marginTop="14dp"
                android:background="#ffffff"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="Enter Your Name"
                android:layout_alignRight="@+id/textView2"
                android:layout_alignEnd="@+id/textView2" />

            <Button
                android:id="@+id/button"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="cancel"
                android:onClick="cancel"
                android:textSize="18dp"
                android:layout_alignBaseline="@+id/button2"
                android:layout_alignBottom="@+id/button2"
                android:layout_alignRight="@+id/editText6"
                android:layout_alignEnd="@+id/editText6" />
        </RelativeLayout>
    </ScrollView>

</FrameLayout>

当我运行它时,它会显示此错误

Error:(168, 1) error: class, interface, or enum expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

并在 myasyncDemo 类的 try 块中的 java 文件中显示建议/错误,我已在其注释行中指定。

请帮我解决这个问题。

【问题讨论】:

  • 清理并重建,一切顺利
  • 不工作。当我尝试重建它时,它给出了同样的错误。
  • 需要更多信息,帮不上忙。它说您在 (168,1) 中有错误
  • stackoverflow.com/help/someone-answers,你接受一个好的答案可以获得声望分

标签: php android android-fragments android-asynctask


【解决方案1】:

1.清理项目。 2.重建项目。 3.使缓存失效并重启android studio

【讨论】:

  • 不工作。当我尝试重建它时,它给出了同样的错误。
  • 好吧尝试其他方式,我认为这与缺少大括号有关,或者大括号在开始此方法定义之前关闭了类主体,请检查。 @苛刻
猜你喜欢
  • 2017-04-20
  • 1970-01-01
  • 2015-03-03
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 2015-03-03
相关资源
最近更新 更多