gpt4 book ai didi

java - 尝试获取 deviceID 时获取 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:02:54 24 4
gpt4 key购买 nike

我正在尝试获取设备ID,以便在我的设备上使用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)

最佳答案

由于您的 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;
}

关于java - 尝试获取 deviceID 时获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com