我试图通过findViewById()和SetVisiblity()更改线性布局的可见性。但是当我调试时,我只看到空值。
请在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>
答案 0 :(得分: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);
}
BackgroundMainActivity.java中的onPostExecute()
a obj = new a();
obj.makeVisible((ForgotPasswordActivity) context);
答案 1 :(得分: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...