在我的项目中,我有一个简单的类来检索location..it有一个静态片段作为内部类。
MainActivity.java
public class MainActivity extends Activity {
GPSTracker gps;
double mylatitude, mylongitude;
TextView txt_location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_location = (TextView) findViewById(R.id.current_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
gps = new GPSTracker(MainActivity.this);
if (gps.canGetLocation()) {
mylatitude = gps.getLatitude();
mylongitude = gps.getLongitude();
String latis = Double.toString(mylatitude);
String longis = Double.toString(mylongitude);
Log.d("my",latis+ "location to string is done " + longis);
txt_location.setText(latis+longis);
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container,
false);
}
}
}
代码在NullPointerException
显示txt_location.setText(latis+longis);
。
Logcat日志如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
at dalvik.system.NativeStart.main(Native Method)
我发现该错误是因为它无法从txt_places
找到(TextView) findViewById(R.id.current_location);
(XML文件中没有任何错误),因为片段的xml中的视图不在Activity中。如何在Activity中访问它?
答案 0 :(得分:1)
由于您在片段中有TextView
,并且活动中的Location
相关方法完全错误。将所有这些移动到一个地方。在片段内移动Location
相关方法,或将布局从片段移动到活动。
类似这样的事情
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
GPSTracker gps;
double mylatitude, mylongitude;
TextView txt_location;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
txt_location = (TextView) view.findViewById(R.id.current_location);
gps = new GPSTracker(getActivity());
if (gps.canGetLocation()) {
mylatitude = gps.getLatitude();
mylongitude = gps.getLongitude();
String latis = Double.toString(mylatitude);
String longis = Double.toString(mylongitude);
Log.d("my",latis+ "location to string is done " + longis);
txt_location.setText(latis+longis);
}
return view;
}
}
}
答案 1 :(得分:1)
修改onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
txt_location = (TextView) view.findViewById(R.id.current_location);
return view;
}
答案 2 :(得分:1)
我的旁注作为答案,正如OP要求的那样。
恕我直言,从Fragment
所附的Activity
更改Fragment
视图是一个糟糕的设计。
应该从Activity
Fragment
调用一个方法,以便Activity
始终处理自己的UI,而不是其父Activity
。
这意味着您的MainActivity
(假设为Fragment
)会将变量保留为MyFragmentA
s(假设MyFragmentB
和MainActivity
)。
要更新任何Fragment
的{{1}}的用户界面,您应该调用一个可以完成工作的方法:myFragmentA.updateUI()
。
因此Fragment
只会真正触及其UI。
它不是MVC设计模式,但无论如何都更干净。
在您的情况下,TextView txt_location
似乎是View
内的Fragment
,因此变量txt_location
本身不应在Activity
中声明,而是它显示在Fragment
。
Activity
(onCreate()
,onResume()
等)或Fragment
(onCreate()
,{{ 1}},onCreateView()
等)不同。<强>答案强>
同样,在你的情况下,onViewCreated()
从(TextView) findViewById(R.id.current_location)
的角度来看是没有意义的,因为Activity
不在其视野中,而是在其中一个孩子中:{{1 }}
将current_location
放在Fragment
的{{1}}中,并使用txt_location = (TextView) view.findViewById(R.id.current_location);
或更晚的onCreateView()
(再次在Fragment
中)。
答案 3 :(得分:0)
这很容易。只需在findviewbyid之前添加getActivity(),例如:
EditText et = (EditText) getActivity().findViewById(R.id.toolbarsrch);