所以我有一个Fragment Activity,它使用另一个片段键盘向地图添加一个标记,并且我得到一个空指针异常。我还在其主机片段中访问键盘以将数据添加到列表中。要清楚键盘正在被两个片段同时使用。 我的代码(这是我从另一个片段访问键盘的片段):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final EditText textLat = (EditText)findViewById(R.id.textLat);
final EditText textLong = (EditText)findViewById(R.id.textLong);
EditText PO = (EditText)findViewById(R.id.POdata);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMyLocationEnabled(true);
/*this line throws the error*/PO.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
int Latitudei = Integer.parseInt(textLat.getText().toString());
int Logitudei = Integer.parseInt(textLong.getText().toString());
mMap.addMarker(new MarkerOptions().position(new LatLng(Latitudei, Logitudei)));
}
return false;
}
});
}
这是键盘片段
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
final EditText PO = (EditText)getActivity().findViewById(R.id.POdata);
EditText textLat = (EditText)getActivity().findViewById(R.id.textLat);
EditText textLong = (EditText)getActivity().findViewById(R.id.textLong);
ListView data = (ListView)getActivity().findViewById(R.id.listView);
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
POin);
data.setAdapter(arrayAdapter);
PO.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
POin.add(PO.getText().toString());
}
return false;
}
});
}
}
堆栈跟踪:
Caused by: java.lang.NullPointerException
at com.example.adrian.xxxxxxxxxx.MapsActivity.onCreate(MapsActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
A.B.C形式的行上的空指针异常意味着点左侧的某些内容为空。在这个例子中:
A为空;
或A.B为空。
没有其他原因。因此,由于textView.getText()永远不会返回null,唯一的可能是用户为null。