如何在操作栏中解决此错误?

时间:2016-05-05 10:37:38

标签: android android-actionbar

我的操作栏有问题。 我正在改变颜色或动作栏。 但是有一个错误。 我无法理解如何解决这个问题。 这是我的java文件的代码。

LoginActivity.java

public class LoginActivity extends Activity implements OnClickListener,AllInOneAsyncTask.ServiceResultListener {

    private EditText loginUserId,loginPassword;
    private Button btnLogin;
    private TextView tvForgetPassword;
    private AppSharedPreference appSharedPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));

        appSharedPreference = AppSharedPreference.getInstance(this);
        initView();
    }

    private void initView()
    {
        loginUserId =(EditText)findViewById(R.id.login_userid);
        loginPassword=(EditText)findViewById(R.id.login_password);

        btnLogin=(Button)findViewById(R.id.btn_login);

        tvForgetPassword=(TextView)findViewById(R.id.tv_forgetpassword);

        btnLogin.setOnClickListener(this);
        tvForgetPassword.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btn_login:

            checkValidation();
            break;

        case R.id.tv_forgetpassword:

            startActivity(new Intent(this,ForgetPasswordActivity.class));
            break;
        }
    }

    @SuppressWarnings("unchecked")
    private void callLoginApi() {
        String string_loginUserId = loginUserId.getText().toString();
        String string_loginPassword = loginPassword.getText().toString();

        Map<String, String> param = null;
        try {
            param = new HashMap<String, String>();
            param.put("username", string_loginUserId);
            param.put("trpass", string_loginPassword);
            param.put("action", "LOGIN");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        String jobjstr = param.toString();
        Log.e("LOGINREQUESTTTTTTT", jobjstr);
        if (jobjstr != null) {
            AllInOneAsyncTask asyncTask = new AllInOneAsyncTask(
                    LoginActivity.this);
            asyncTask.setServiceResultListener(this);
            asyncTask.setServiceType(Constants.SERVICE_TYPE_GET_PROFILE);
            asyncTask.execute(param);
        }
    }

    private void checkValidation()
    {
        String string_loginUserId = loginUserId.getText().toString();
        String string_loginPassword = loginPassword.getText().toString();

        if(!string_loginUserId.matches(""))
        {
            if(string_loginPassword.length()>=6)
            {
                callLoginApi();
            }
            else
            {
                AppUtil.DefaultPopup(this, getResources().getString(R.string.wrong_password));
            }
        }
        else
        {
            AppUtil.DefaultPopup(this,getResources().getString(R.string.wrong_usrId));
        }
    }

    @Override
    public void onResult(String resultData, int requestType) {
        // TODO Auto-generated method stub
        if (requestType == Constants.SERVICE_TYPE_GET_PROFILE) {
            try {
                JSONObject obj = new JSONObject(resultData);
                String status = obj.getString("Status");
                String message = obj.getString("Message");
                if (status.equals("1")) {
                    String string_loginUserId = loginUserId.getText().toString();
                    String string_loginPassword = loginPassword.getText().toString();

                    String balance = obj.getString("Balance");
                    String name = obj.getString("Name");
                    String mobile = obj.getString("Mobile");
                    String email = obj.getString("Email");

                    appSharedPreference.setBalance(balance);
                    appSharedPreference.setName(name);
                    appSharedPreference.setMobile(mobile);
                    appSharedPreference.setEmail(email);
                    appSharedPreference.setUserName(string_loginUserId);
                    appSharedPreference.setPassword(string_loginPassword);

                    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(this,MainActivity.class));
                    finish();
                }
                else {
                    AppUtil.DefaultPopup(this,message);
                }
            }
            catch(Exception e)
            {

            }
        }
    }

activity_login.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/light_grayish_blue" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="8dp"
        android:text="LOGIN IN MY ACCOUNT"
        android:textColor="#F26341"
        android:textSize="30sp"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_below="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="20dp">

    <EditText
        android:inputType="textEmailAddress"
        android:hint="@string/tx_user"
        style="@style/EditTextStyle"
        android:background="@drawable/edittext_bg"
        android:id="@+id/login_userid" />

    <EditText
        android:inputType="textPassword"
        android:hint="@string/tx_password"
        style="@style/EditTextStyle"
        android:background="@drawable/edittext_bg"
        android:id="@+id/login_password"/>

    <Button
        style="@style/ButtonStyle"
        android:id="@+id/btn_login"
        android:background="@drawable/button_selector"
        android:text="@string/tx_login"
        android:textSize="20sp"
        android:textColor="@color/white"/>

        <TextView
            android:id="@+id/tv_forgetpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingTop="10dp"
            android:text="@string/tx_forgotpassword"
            android:textColor="@color/black"
            android:textSize="20sp" />

    </LinearLayout>

</RelativeLayout>

我得到一个例外。

FATAL EXCEPTION: main
        Process: com.webzone.allbillpay, PID: 11361
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webzone.allbillpay/com.webzone.allbillpay.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
        at com.webzone.allbillpay.LoginActivity.onCreate(LoginActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:6010)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

3 个答案:

答案 0 :(得分:0)

必须在没有操作栏的情况下设置应用程序的默认主题,以便在操作栏上显示为null。

最简单的解决方法是在你宣布活动标签的清单文件中,只需添加此属性

android:theme="@android:style/Theme.WithActionBar"

答案 1 :(得分:-1)

您的LoginActivity主题没有动作栏。检查清单中提到的主题。

由于活动没有操作栏,getActionBar()返回null值。并且

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));

导致崩溃

这样做

if(getActionBar() != null) {
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));
}

或更改活动的主题

<activity
            android:name=".LoginActivity"
            android:theme="@style/FullscreenTheme">

删除android:theme

答案 2 :(得分:-1)

导航到xml文件中的工具栏并替换为

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

这会将工具栏颜色更改为colorAccent

在java文件中评论此部分。上面的xml文件可以解决问题。

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));

在工具栏颜色更改之前

enter image description here

在app_bar_main.xml中更改颜色后,下面是工具栏

enter image description here


使用的主题是

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>