簡體   English   中英

在嘗試獲取deviceID時獲取NullPointerException

[英]Getting NullPointerException, while trying to get deviceID

我正在嘗試獲取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)

由於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;
    }

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM