簡體   English   中英

Android Studio:editText的獲取器

[英]Android Studio: Getters for editText

我想在另一個活動中使用2個editTexts的值。 到目前為止,這是我的代碼。 我正進入(狀態:

顯示java.lang.NullPointerException。

編碼:

public class AddJob extends AppCompatActivity{
    // vars
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;

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

        TextView textView = findViewById(R.id.activityTitleAddJob);
        textView.setText("Add a Job");

        editTextLat = findViewById(R.id.editTextLat);
        editTextLng = findViewById(R.id.editTextLng);
    }

    public int getLatitude() {
        return new Integer(editTextLat.getText().toString());
    }

    public int getLongitude() {
        return new Integer(editTextLng.getText().toString());
    }
}

堆棧跟蹤:

在此處輸入圖片說明

這是map類的代碼片段:

AddJob aj = new AddJob();
int lat = aj.getLatitude();
int lng = aj.getLongitude();
Toast.makeText(aj, lat + " " + lng, Toast.LENGTH_LONG).show();

請閱讀有關活動生命周期的信息 絕對不應直接使用

 new MyActivity() 

這不會啟動任何生命周期事件( onCreate等),也不會將其綁定到上下文,在其上設置視圖層次結構,也不會執行您可能期望的任何常規活動。 您的程序返回null,因為從未在活動上調用onCreate ,並且如果您只是嘗試自己嘗試調用它,則可能會崩潰。

如果您希望一個活動中的數據在另一個活動中可用,那么一種簡單的方法是將數據保存在AddJob活動中的SharedPreferences中(無論何時更新值),然后從SharedPreferences中的MapActivity訪問它。 您還可以在啟動時將數據添加到Intent中,從而將數據從一個Activity傳遞到下一個Activity。

在此處使用SharedPreferences的一個優點是,用戶的選擇將從一個應用程序會話保存到下一個會話,並且如果您有多個可以啟動MapActivity東西,則不必繼續將數據傳遞給它。

你好,使用Intent會不會更好

Intent i = new Intent(MainActivity.this, NEXTActivity.class);
            i.putExtra("latitude", lat);
            i.putExtra("longitude", lng);
            startActivity(i);

NEXTActivity

 Bundle extras = getIntent().getExtras();
    double latitude = extras.getDouble("latitude");
    double longitude = extras.getDouble("longitude");

您不應該創建活動對象,可以使用Intent啟動活動並通過它傳遞數據。 在下面的鏈接中查看相關答案:

https://stackoverflow.com/a/2091482/10116426

看來您正在實例化AddJob類。 這可能會在android的后堆棧上引發問題,這可能會導致生命周期管理問題。 因此,正如@NoobAndroid所提到的,最好使用推薦的方法,以免發生意外錯誤。

use this code .

package com.example.cloudanalogy.introscreen;

import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                TextView textView = findViewById(R.id.activityTitleAddJob);
                textView.setText("Add a Job");
                editTextLat = findViewById(R.id.editTextLat);
                editTextLng = findViewById(R.id.editTextLng);
        }

            public int getLatitude() {
                return Integer.parseInt(editTextLat.getText().toString());
            }

            public int getLongitude() {
                return Integer.parseInt(editTextLng.getText().toString());
            }

    public void onclick(View view) {

        int lat = getLatitude();
        int lng = getLongitude();
        Toast.makeText(this, ""+lat+" and "+lng, Toast.LENGTH_SHORT).show();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM