[英]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启动活动并通过它传递数据。 在下面的链接中查看相关答案:
看来您正在实例化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.