java - 为什么我的 Activity 参数给我必需的;

标签 java android android-intent

我现在正在尝试将数据从一个 Activity 发送到另一个 Activity ,我现在想知道为什么 int requestCode、int resultCode 告诉我这些变量从未使用过,而且为什么它们会因此变成红色?这也给了我一个需要; ?我正在接受本教程的帮助https://codelabs.developers.google.com/codelabs/android-room-with-a-view/index.html?index=..%2F..%2Findex#13我看不到他在哪里添加了请求代码?也许你们可以找到它...谢谢您的帮助

package com.example.jenso.paperseller;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    CustomerDatabase database;
    FloatingActionButton fab;
    private CustomerViewModel mCustomerViewModel;
    final int NEW_CUSTOMER_ACTIVITY_REQUEST_CODE = 1;



    private static final  String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView recyclerView = findViewById(R.id.recycler);
        final PapperRecyclerAdapter adapter = new PapperRecyclerAdapter(this);


        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        mCustomerViewModel = ViewModelProviders.of(this).get(CustomerViewModel.class);

        mCustomerViewModel.getmAllCustomers().observe(this, new Observer<List<Customer>>() {
            @Override
            public void onChanged(@Nullable List<Customer> customers) {
                adapter.setCustomer(customers);
            }
        });
        public void onActivityResult(int requestCode; int resultCode; Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == NEW_CUSTOMER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
                Customer customer = new Customer(data.getStringExtra(CreateCustomer.EXTRA_REPLY), data.getStringExtra(CreateCustomer.SECOND_REPLY),data.getStringExtra(CreateCustomer.THIRD_REPLY),data.getStringExtra(CreateCustomer.FOURTH_REPLY));
                mCustomerViewModel.insert(customer);
            } else {
                Toast.makeText(
                        getApplicationContext(),
                        R.string.empty_not_saved,
                        Toast.LENGTH_LONG).show();
            }
        }

        recyclerView.setAdapter(adapter);

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: Do this when you click");
                Intent intent = new Intent(MainActivity.this, CreateCustomer.class);
                startActivityForResult(intent, NEW_CUSTOMER_ACTIVITY_REQUEST_CODE);


            }

        });
















    }







}

最佳答案

好像你把onActivityResult方法写在了onCreate方法里面,请把它写在onCreate方法外面。 希望这有效..!

关于java - 为什么我的 Activity 参数给我必需的;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303482/

相关文章:

java - 为什么 sonar-maven-plugin 在加载全局设置或 Activity 规则时挂起?

java - ObjectStore Bluemix 连接 java

android - BottomSheetBehavior View 不是 CoordinatorLayout 的 subview

Android MediaPlayer 无法使用 Uri.parse '#' 播放文件名中包含 ("...#.mp3"字符的文件)

java - 准备好的声明有问题

java - 如何在 Mule 中的异步 AMQP-RabbitMQ 实现中禁用 tempQueues?

java - 在 iOS/Android 和 Firebase 中处理 Date 的最佳方法是什么?

android - 如何实现 'first-use'/教程 Activity

java - Android getIntent() 空指针异常

java - Android 应用程序中的 Google Plus 集成