将数据从一项活动传递到另一项无效

时间:2019-04-21 08:58:48

标签: android

settings.java

public class settingsActivity extends MainActivity  {


    private TextView mTextMessage;
    Intent i=new Intent();


    Button saveButton;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            goalDate = findViewById(R.id.goaldinput);
            goalWeight = findViewById(R.id.goalwinput);
            nameIn = findViewById(R.id.nameinput);
            fT=findViewById(R.id.feet);
            iNches=findViewById(R.id.inches);
            mAle=findViewById(R.id.maleButton);
            feMale=findViewById(R.id.othersButton);
            oThers=findViewById(R.id.femaleButton);
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(settingsActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(settingsActivity.this, historyActivity.class);
                    startActivity(intent2);

                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(settingsActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(settingsActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        saveButton=findViewById(R.id.saveButton);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText goalD = ((EditText)findViewById(R.id.goaldinput));
                EditText goalW = ((EditText)findViewById(R.id.goalwinput));
                EditText currentW = ((EditText)findViewById(R.id.currentWinput));
                i = new Intent(settingsActivity.this,historyActivity.class);
                gD=goalD.getText().toString();
                gW=goalW.getText().toString();
                cW=currentW.getText().toString();
                i.putExtra("goalDate",gD);
                i.putExtra("goalWeight",gW);
                i.putExtra("currentWeight",cW);
                startActivity(i);

/*
                arrayList1.add(goalD.toString());
                arrayList2.add(goalW.toString());
                arrayList3.add(currentW.toString());
*/


//                adapter1.notifyDataSetChanged();
              //  adapter2.notifyDataSetChanged();
               // adapter3.notifyDataSetChanged();

            }
        });


    }


history.java

   public class historyActivity extends MainActivity {
    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {



            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(historyActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(historyActivity.this, historyActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(historyActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(historyActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        listView1= findViewById(R.id.goaldlistView);
        listView2= findViewById(R.id.goalwlistView);
        listView3= findViewById(R.id.currentwlistView);

        EditText goalD = ((EditText)findViewById(R.id.goaldinput));
        EditText goalW = ((EditText)findViewById(R.id.goalwinput));
        EditText currentW = ((EditText)findViewById(R.id.currentWinput));

       gDate = getIntent().getExtras().getString("goalDate");
        gWeight = getIntent().getExtras().getString("goalWeight");
        cWeight = getIntent().getExtras().getString("currentWeight");

        String addArray1[] = {"3/14/1992"};
        String addArray2[] = {"152"};
        String addArray3[] = {"160"};

        arrayList1=new ArrayList<String>(Arrays.asList(addArray1));
        arrayList2=new ArrayList<String>(Arrays.asList(addArray2));
        arrayList3=new ArrayList<String>(Arrays.asList(addArray3));
        arrayList1.add(gDate);
        arrayList2.add(gWeight);
        arrayList3.add(cWeight);
        adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList1);
        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList2);
        adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList3);

        listView1.setAdapter(adapter1);
        listView2.setAdapter(adapter2);
        listView3.setAdapter(adapter3);



    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    EditText goalDate ;
    EditText goalWeight ;
    EditText nameIn ;
    EditText fT;
    EditText iNches;
    EditText currentWeight;
    RadioButton mAle;
    RadioButton feMale;
    RadioButton oThers;

    public ArrayList<String>arrayList1=new ArrayList<String>();
    public ArrayList<String>arrayList2=new ArrayList<String>();
    public ArrayList<String>arrayList3=new ArrayList<String>();

    public ArrayAdapter<String> adapter1;
    public ArrayAdapter<String> adapter2;
    public ArrayAdapter<String> adapter3;

    Intent intent =new Intent();

    public ListView listView1;
    public ListView listView2;
    public ListView listView3;


    String gD=" ";
    String gW=" ";
    String cW=" ";
    String gDate=" ";
    String gWeight=" ";
    String cWeight=" ";

    ;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(MainActivity.this, historyActivity.class);
                    startActivity(intent2);


                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(MainActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(MainActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

将数据从设置传递到历史记录时出现以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

gDate是错误的bundle的nullException错误,我认为它可能适用于gWeight和cWeight

我尝试初始化MainActivity,Setting和History文件中的意图。仍然没有传递数据。

需要传递“设置”中的输入并显示在“历史记录”中的listView中

1 个答案:

答案 0 :(得分:0)

您不需要写

Intent intent = new Intent();在您的“历史记录”课程中。

代替这篇文章。

Intent intent = getIntent();

gDate = intent.getExtras().getString("goalDate");
gWeight = intent.getExtras().getString("goalWeight");
cWeight = intent.getExtras().getString("currentWeight");

-更新-

删除意图i = new Intent();来自“设置”活动的父级。

在onCreate()中做到这一点。

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new Intent(settingsActivity.this,historyActivity.class);
            String gD = goalD.getText().toString();
            String gW = goalW.getText().toString();
            String cW = currentW.getText().toString();
            i.putExtra("goalDate",gD);
            i.putExtra("goalWeight",gW);
            i.putExtra("currentWeight",cW);
            startActivity(i);
        }
    });

在“历史记录”活动中:

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

    Intent intent = getIntent();
    String gDate = intent.getExtras().getString("goalDate");
    String gWeight = intent.getExtras().getString("goalWeight");
    String cWeight = intent.getExtras().getString("currentWeight");

    // OR

    Intent intent = getIntent();
    String gDate = intent.getStringExtra("goalDate");
    String gWeight = intent.getStringExtra("goalWeight");
    String cWeight = intent.getStringExtra("currentWeight");

}

我也建议您添加支票。

if(intent.getExtras() != null) {
   //Your code here.
}

-更新2 ---

您在评论中说:“但是当我按下导航工具栏中的历史记录按钮时,该程序将在nullPointerException上退出”。这是正常现象,因为单击导航工具栏中的“历史记录”按钮后,此代码才可以在设置片段中使用。

case R.id.navigation_history:
     mTextMessage.setText(R.string.title_history);
     Intent intent2 = new Intent(settingsActivity.this, historyActivity.class);
     startActivity(intent2);

     break;

您看到这里没有调用任何putExtra(),因此当它打开历史记录时,它将通过nullPointerException,因为您在历史记录类中调用了getExtra(),但是由于您没有添加任何额外的内容,因此getExtra()将返回null。因此,防止这种情况的唯一方法是在这种情况下添加putExtra()或在获得额外附加值之前进行检查。

Intent intent = getIntent();

if(intent.getExtras() != null) {  //Also there is hasExtra() check too, but I'd like to check like this.
  gDate = intent.getExtras().getString("goalDate");
  gWeight = intent.getExtras().getString("goalWeight");
  cWeight = intent.getExtras().getString("currentWeight");
}