2017-06-09 55 views
-1

我試圖通過findViewById()然後SetVisiblity()更改線性佈局的可見性。但是,當我調試時,我只看到空值。AsyncTask,LinearLayout Android更新可見性

請在onPostExecute()方法的BackgroundMainActivity.java下面找到代碼片段。

我評論過它。請幫忙。

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); 
    } 
} 

編輯:

activity_main.xml中

ss_layout_questions是默認了。我想在BackgroundMainActivity.java中將其設置爲「result.equals(」success「)」。

<?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

你是什麼意思「僅爲空值」?那是NullPointerException?你可以發表一些日誌請 –

+0

BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this,v);不要直接傳遞v。從佈局文件中取出視圖對象。 – Abhishek

+0

viewroot來自onClick()的v參數,它似乎設置在按鈕右邊?這意味着viewroot是被點擊的按鈕,並且您正在按鈕上調用findViewById。這是你打算做什麼? – Juan

回答

0

它終於工作了。這是我做的

我在BackgroundMainActivity.java中添加了一個新類,然後在onPostExecute()方法中調用它。

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()在BackgroundMainActivity.java

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

還有一件事要考慮。當您將活動的引用傳遞給AsyncTask時,您應該將活動(上下文)存儲在WeakReference中,因爲可能發生的情況是,當AsyncTask完成時,上下文可能爲空。保持一個引用會在應用程序中引入內存泄漏。 – Juan

0

是的,你應該得到NPE錯誤。爲什麼?因爲看這個代碼:

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()); 
    } 
}); 

在這裏,你試圖傳遞一個Button(你button_getUserName)您AsyncTask。然後,當您找到視圖時,當然,您的按鈕中沒有佈局。

如何解決?實際上,這很容易,只需在AsyncTask的構造函數中傳遞你的佈局即可:

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