单击android时将ProgressBar设置为VISIBLE

时间:2013-04-13 02:36:10

标签: android xml

单击LogIn按钮时,我试图让ProgressBar变为VISIBLE。这是我的活动XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/txtView_UserName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/login_username_hint">

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txtView_Password"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtView_UserName"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:hint="@string/login_password_hint"
    android:inputType="textPassword" />

<Button
    android:id="@+id/button_LogIn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/txtView_UserName"
    android:layout_below="@+id/txtView_Password"
    android:layout_marginTop="10dp"
    android:onClick="logIn"
    android:text="@string/login_button" />

<CheckBox
    android:id="@+id/checkbox_RememberPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtView_Password"
    android:layout_below="@+id/button_Login"
    android:layout_marginTop="15dp"
    android:text="@string/remember_my_password" />

<CheckBox
    android:id="@+id/checkbox_LogMeInAutomatically"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkbox_RememberPassword"
    android:layout_below="@+id/checkbox_RememberPassword"
    android:layout_marginTop="-5dp"
    android:text="@string/log_me_in_automatically" />

<ProgressBar
    android:id="@+id/progressBar_LogIn"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkbox_LogMeInAutomatically"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:visibility="invisible" />

<TextView
    android:id="@+id/txtView_Version"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="18dp"
    android:text="@string/version_number" />

</RelativeLayout>

这就是我试图在代码中将其更改为VISIBLE的地方。

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//Get Views and Buttons
 EditText txt_Username = (EditText) findViewById(R.id.txtView_UserName);
 EditText txt_Password = (EditText) findViewById(R.id.txtView_Password);
 Button btn_LogIn = (Button) findViewById(R.id.button_LogIn);
 CheckBox chkbox_RememberMyPassword = (CheckBox) findViewById(R.id.checkbox_RememberPassword);
 CheckBox chkbox_LogMeInAutomatically = (CheckBox) findViewById(R.id.checkbox_LogMeInAutomatically);
 ProgressBar progressbar_LogIn = (ProgressBar) findViewById(R.id.progressBar_LogIn);    

public void logIn() {
    progressbar_LogIn.setVisibility(0);
}

}

程序崩溃而不渲染我的Activity的视图。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我有一种感觉,因为你有一个onClick登录,但登录不带一个view参数。尝试以编程方式为按钮设置onclick,而不是使用xml。

修改:尝试更改

public void logIn() {
progressbar_LogIn.setVisibility(0);

}

public void logIn(View v) {
progressbar_LogIn.setVisibility(0);

}

或以编程方式设置

btn_LogIn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            //CODE GOES HERE
        }
    });

进一步编辑:

还注意到没有onCreateView的迹象,所以要确保所有视图代码都在onCreate内部或至少内部:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
}