登录屏幕 - Recycler View - 设备进入睡眠状态时禁用服务

时间:2018-04-19 08:38:14

标签: android android-recyclerview android-service android-broadcastreceiver sleep-mode

我正在尝试使用2个屏幕创建一个应用程序:MainActivity(登录)和CustomerInfoActivity。

  • 登录界面。

  • 另一个屏幕用于显示回收站视图显示的客户信息(姓名,电话,图像),以及用于显示位置更新的文本视图。

  • 使用广播接收器并在设备进入睡眠模式/唤醒时捕获事件=>禁用/启用服务。

当我单击“登录”按钮跳转到“回收”视图屏幕时,我的应用程序崩溃,我不知道如何处理禁用服务要求。

请给我一些帮助。谢谢。

DataCustomer.java

public class DataCustomer {
private int image;
private String name;
private int phonenumber;

public DataCustomer(String name, int phonenumber, int image) {
    this.image = image;
    this.name = name;
    this.phonenumber = phonenumber;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPhonenumber() {
    return phonenumber;
}

public void setPhonenumber(int phonenumber) {
    this.phonenumber = phonenumber;
}}

CustomerAdapter.java

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.ViewHolder> {
private ArrayList<DataCustomer> dataCustomers;
private Context context;

public CustomerAdapter(ArrayList<DataCustomer> dataCustomers, Context context) {
    this.dataCustomers = dataCustomers;
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View itemView = layoutInflater.inflate(R.layout.item_customer,parent,false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.customer_name.setText(dataCustomers.get(position).getName());
    holder.customer_phone.setText(dataCustomers.get(position).getPhonenumber());
    holder.customer_image.setImageResource(dataCustomers.get(position).getImage());

}

@Override
public int getItemCount() {
    return dataCustomers.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView customer_name;
    TextView customer_phone;
    ImageView customer_image;

    public ViewHolder(View itemView){
        super(itemView);
        customer_name = (TextView)itemView.findViewById(R.id.customer_name);
        customer_phone = (TextView) itemView.findViewById(R.id.customer_phone);
        customer_image = (ImageView)itemView.findViewById(R.id.customer_image);

    }
}}

CustomerInfoActivity.java

public class CustomerInfoActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customer_info);
    initView();
}
public void initView(){
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.customer_items);
    recyclerView.setHasFixedSize(true);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(layoutManager);
    ArrayList<DataCustomer> arrayList = new ArrayList<>();

    arrayList.add(new DataCustomer("A",12,R.drawable.A));
    arrayList.add(new DataCustomer("B",34,R.drawable.B));
    arrayList.add(new DataCustomer("C",56,R.drawable.C));
    arrayList.add(new DataCustomer("D",78,R.drawable.D));

    CustomerAdapter customerAdapter = new CustomerAdapter(arrayList, getApplicationContext());
    recyclerView.setAdapter(customerAdapter);
}}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static CheckBox rememberpassword;
private static Button loginbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    loginButton();

}
public void loginButton(){
    username = (EditText)findViewById(R.id.editText_username);
    password = (EditText)findViewById(R.id.editText_password);
    rememberpassword = (CheckBox)findViewById(R.id.checkBox_rememberpassword);
    loginbutton = (Button)findViewById(R.id.button_login);

    loginbutton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(username.getText().toString().equals("user") &&
                            password.getText().toString().equals("pass")  ) {

                        Intent intent = new Intent("com.assignment.assignment2_v2.CustomerInfoActivity");
                        startActivity(intent);
                    }
                }
            }
    );
}
}

1 个答案:

答案 0 :(得分:0)

可能可以通过将隐式@PostMapping("/user/profile") public ResponseEntity<?> saveProfile(@Valid @RequestBody UserProfileDTO userProfile,Principal userLogin) { String username=userLogin.getName(); return username; } 切换为显式错误来修复崩溃:

Intent

试试这个。如果这不起作用,您还可以添加崩溃日志吗?