2017-06-09 3 views
-1

Ich versuche die Sichtbarkeit eines linearen Layouts durch findViewById() und dann SetVisiblity() zu ändern. Aber wenn ich debugge, sehe ich nur Nullwerte.AsyncTask, LinearLayout Android Update Sichtbarkeit

Finden Sie das Snippet in der BackgroundMainActivity.java unten in der onPostExecute() Methode.

Ich habe es kommentiert. Bitte helfen Sie.

MainActivity.java

button_getUsername.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, v); 
      backgroundMainActivity.execute("random", editText_username.getText().toString()); 
     } 
    }); 

BackgroundMainActivity.java

public class BackgroundMainActivity extends AsyncTask<String, Void , String> { 


    public BackgroundMainActivity(Context ct, View view) { 
     context = ct; 
     rootView = view; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String login_URL = "some url"; 
     try { 
      String username = params[1]; 

      URL url = new URL(login_URL); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setDoOutput(true); 

      OutputStream outputStream = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
      String postData = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8"); 
      bufferedWriter.write(postData); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      outputStream.close(); 

      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 

      result = bufferedReader.readLine(); 

      bufferedReader.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return result; 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute(){ 

    } 

    @Override 
    protected void onPostExecute (String result){ 
     if(result.equals("success")){ 

//need to make this change 
//LinearLayout someLayout = (LinearLayout)rootview.findViewById(R.id.layout_content); 
//someLayout.setVisibility(View.VISIBLE); 

      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setMessage(result) 
        .setPositiveButton("Ok",null) 
        .show(); 
     } 

    } 

    @Override 
    protected void onProgressUpdate(Void... values){ 
     super.onProgressUpdate(values); 
    } 
} 

EDIT:

activity_main.xml

ss_layout_questions ist standardmäßig gegangen. Ich möchte es in BackgroundMainActivity.java einmal "result.equals (" success ")" sichtbar machen.

<?xml version="1.0" encoding="utf-8"?> 
<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="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context="com.xtremustechnologies.chotu.useractivity.MainActivity"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     android:background="@color/lightGreen" 
     android:layout_gravity="top"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignEnd="@+id/textviewr2" 
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="30dp" 
        android:text="CHOTU" 
        android:textColor="@color/white" 
        android:textSize="40dp" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginBottom="40dp" 
        android:text="Your Personal Assistant" 
        android:textColor="@color/white" 
        android:textSize="12dp" /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/ss_textview_questions" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginBottom="40dp" 
       android:text="Security Questions " 
       android:textColor="@color/black" 
       android:textSize="20dp" 
       android:visibility="gone"/> 

      <EditText 
       android:id="@+id/ss_et_username" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="center_horizontal" 
       android:hint="Enter Username" /> 

      <Button 
       android:id="@+id/ss_button_getusername" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/ss_et_username" 
       android:layout_centerHorizontal="true" 
       android:onClick="clickGetUsername" 
       android:layout_gravity="center_horizontal" 
       android:text="Submit" /> 

      <LinearLayout 
       android:id="@+id/ss_layout_questions" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:visibility="gone"> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/textviewr3" 
        android:layout_centerHorizontal="false" 
        android:layout_marginLeft="40dp" 
        android:layout_marginRight="40dp" 
        android:orientation="horizontal"> 

        <Button 
         android:id="@+id/ss_button_dob" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="left" 
         android:layout_marginRight="50dp" 
         android:text="Date of Birth" /> 

        <TextView 
         android:id="@+id/ss_textView_dob" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="right|center_vertical|center_horizontal" 
         android:layout_weight="1" 
         android:text="TextView" 
         android:textSize="20dp" /> 
       </LinearLayout> 

       <TextView 
        android:id="@+id/ss_textView_question1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="30dp" /> 

       <EditText 
        android:id="@+id/ss_textView_ans1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginTop="10dp" 
        android:hint="Enter your answer here" /> 

       <TextView 
        android:id="@+id/ss_textView_question2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="30dp" /> 

       <EditText 
        android:id="@+id/ss_textView_ans2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginTop="10dp" 
        android:hint="Enter your answer here" /> 

       <TextView 
        android:id="@+id/ss_textView_question3" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="30dp" /> 

       <EditText 
        android:id="@+id/ss_textView_ans3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginTop="10dp" 
        android:hint="Enter your answer here" /> 

       <Button 
        android:id="@+id/ss_button_submit" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:background="@color/black" 
        android:text="Submit" 
        android:textColor="@color/white" /> 
      </LinearLayout> 
     </LinearLayout> 


    </android.support.v4.widget.NestedScrollView> 
</android.support.constraint.ConstraintLayout> 
+2

was meinst du mit „Null-Werte nur.“? ist das NullPointerException? Kannst du bitte ein Protokoll posten? –

+0

BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity (MainActivity.this, v); Übergeben Sie v nicht direkt. Nehmen Sie View Object aus der Layoutdatei. – Abhishek

+0

viewroot stammt von dem v-Parameter von onClick(), der auf einer Schaltfläche gesetzt zu sein scheint, richtig? Das würde bedeuten, dass Viewroot die Schaltfläche ist, auf die geklickt wurde, und Sie findViewById auf einer Schaltfläche aufrufen. Wollen Sie das tun? – Juan

Antwort

0

Es funktioniert endlich. Hier ist, was ich getan habe

Ich habe eine neue Klasse in BackgroundMainActivity.java hinzugefügt und dann in der onPostExecute() -Methode aufgerufen.

class a{ 

public Button button_reset, button_dob; 
public LinearLayout layout_questions; 
public TextView textView_title; 
public EditText editText_username; 

public void makeVisible(ForgotPasswordActivity context){ 

    LinearLayout layout_questions = (LinearLayout) context.findViewById(R.id.ss_layout_questions); 
    layout_questions.setVisibility(View.VISIBLE); 
} 

OnPostExecute() in BackgroundMainActivity.java

a obj = new a(); 
     obj.makeVisible((ForgotPasswordActivity) context); 
+0

Eine weitere Sache zu prüfen.Wenn Sie einen Verweis auf die Aktivität an die AsyncTask übergeben, sollten Sie die Aktivität (den Kontext) in einer WeakReference speichern, da der Kontext bei Abschluss der AsyncTask möglicherweise null sein kann. Wenn Sie eine Referenz aufbewahren, wird ein Speicherleck in die App eingeführt. – Juan

0

Ja, Sie sollten NPE Fehler. Warum? Da dieser Code sehen:

button_getUsername.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, v); 
     backgroundMainActivity.execute("random", editText_username.getText().toString()); 
    } 
}); 

Hier versuchen Sie eine Button (Ihr button_getUserName), um Ihre AsyncTask zu übergeben. Dann, wenn Sie die Ansicht finden, ist natürlich kein Layout in Ihrer Schaltfläche.

Wie löst man es? Eigentlich ist es sehr einfach, nur das Layout passieren in den Konstruktor der AsyncTask als:

BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, findViewById(R.id.layout_content)); 
// do on onPost:v.setVisibility...