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>
was meinst du mit „Null-Werte nur.“? ist das NullPointerException? Kannst du bitte ein Protokoll posten? –
BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity (MainActivity.this, v); Übergeben Sie v nicht direkt. Nehmen Sie View Object aus der Layoutdatei. – Abhishek
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