我試圖通過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>
你是什麼意思「僅爲空值」?那是NullPointerException?你可以發表一些日誌請 –
BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this,v);不要直接傳遞v。從佈局文件中取出視圖對象。 – Abhishek
viewroot來自onClick()的v參數,它似乎設置在按鈕右邊?這意味着viewroot是被點擊的按鈕,並且您正在按鈕上調用findViewById。這是你打算做什麼? – Juan