无法解析方法setText / getText

时间:2017-08-22 14:48:57

标签: java android

所以我还在学习Android和Java,我试图按照这个页面上的代码进行操作:http://www.androidauthority.com/use-remote-web-api-within-android-app-617869/

问题是他们提供的代码对我不起作用,Android Studio说无法解析setText和getText方法的方法。

这是我的代码:

    package com.example.beldr.apitest;

    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ProgressBar;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import static com.example.beldr.apitest.R.id.emailText;
    import static com.example.beldr.apitest.R.id.responseView;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    }

    class RetrieveFeedTask extends AsyncTask<Void, Void, String> {


    private Exception exception;
    private ProgressBar progressBar;

    protected void onPreExecute() {


        progressBar.setVisibility(View.VISIBLE);
        responseView.setText("");
    }

    protected String doInBackground(Void... urls) {
        String email = emailText.getText().toString();
        // Do some validation here

        try {
            URL url = new URL("https://api.fullcontact.com/v2/person.json" + "email=" + email + "&apiKey=" + "126042055");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();
                return stringBuilder.toString();
            }
            finally{
                urlConnection.disconnect();
            }
        }
        catch(Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return null;
        }
    }

    protected void onPostExecute(String response) {
        if(response == null) {
            response = "THERE WAS AN ERROR";
        }
        progressBar.setVisibility(View.GONE);
        Log.i("INFO", response);
        responseView.setText(response);
    }
}

这是activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.beldr.apitest.MainActivity">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:context="com.sample.foo.simplewebapi.MainActivity"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        <EditText
            android:id="@+id/emailText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="Enter email address"/>
        <Button
            android:id="@+id/queryButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            style="@style/Base.Widget.AppCompat.Button.Borderless"
            android:text="Search"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminate="true"
                android:layout_centerHorizontal="true"
                android:visibility="gone" />
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/responseView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </ScrollView>
        </RelativeLayout>
    </LinearLayout>


</android.support.constraint.ConstraintLayout>

编辑:我试过把

TextView textView=(TextView)findViewById(R.id.responseView); 

但后来它说无法解析findViewById的方法

3 个答案:

答案 0 :(得分:0)

TextView responseView = (TextView) findViewById(R.id.responseView);

然后你就可以.setText()等了

答案 1 :(得分:0)

问题是你使用的是来自id的方法,而不是来自对象。

这应该是这样的:

EditText etEmail = (EditText) findViewById(R.id.emailText);
TextView tvResponse = (TextView) findViewById(R.id.responseView);

然后你可以从etEmail和tvResponse调用getText()和setText()。

奖励:删除这些行

import static com.example.beldr.apitest.R.id.emailText;
    import static com.example.beldr.apitest.R.id.responseView;

只需使用:

import com.example.beldr.apitest.R

答案 2 :(得分:0)

TextView textView = (TextView) findViewById(R.id.responseView); 

进入onCreate()方法并在类中创建变量 TextView textView 全局,这样就可以从AsyncTask中调用它