我正在尝试获取deviceID,在我的设备上使用AdMob(是的,我知道我可以在LogCat中显示它,但它只是一个基本程序,我以为我可以写没有问题),但是由于某种原因我得到NullPointerException。我在布局中添加了deviceid textview,所以我并不完全明白问题所在。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final TextView deviceId = (TextView) findViewById(R.id.device);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
String deviceid = tm.getDeviceId();
deviceId.setText(deviceid);
}
//These are just autogenerated by SDK, so don't think they are matter.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.deviceid.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device ID:" />
<TextView
android:layout_below="@id/text"
android:id="@+id/device"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
如果我在LogCat中正确看到此行存在问题:
deviceId.setText(deviceid);
我该如何解决?
EDIT1:这是我的logcat:
03-20 11:32:25.629: E/AndroidRuntime(873): FATAL EXCEPTION: main
03-20 11:32:25.629: E/AndroidRuntime(873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deviceid/com.example.deviceid.MainActivity}: java.lang.NullPointerException
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.os.Looper.loop(Looper.java:137)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-20 11:32:25.629: E/AndroidRuntime(873): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 11:32:25.629: E/AndroidRuntime(873): at java.lang.reflect.Method.invoke(Method.java:525)
03-20 11:32:25.629: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-20 11:32:25.629: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-20 11:32:25.629: E/AndroidRuntime(873): at dalvik.system.NativeStart.main(Native Method)
03-20 11:32:25.629: E/AndroidRuntime(873): Caused by: java.lang.NullPointerException
03-20 11:32:25.629: E/AndroidRuntime(873): at com.example.deviceid.MainActivity.onCreate(MainActivity.java:38)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.Activity.performCreate(Activity.java:5133)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-20 11:32:25.629: E/AndroidRuntime(873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
答案 0 :(得分:2)
由于TextView属于fragment_main.xml
,因此您可以通过以下方式更改onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final TextView deviceId = (TextView) rootView.findViewById(R.id.device);
final TelephonyManager tm =(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
String deviceid = tm.getDeviceId();
deviceId.setText(deviceid);
}
return rootView;
}
答案 1 :(得分:0)
TextView
属于片段。所以你需要初始化如下
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView deviceId = (TextView) rootView.findViewById(R.id.device);
TelephonyManager tm =(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
deviceId.setText(deviceid);
return rootView;
或onActivityCreated
TextView deviceId = (TextView) getView().findViewById(R.id.device);