我想設置一個TextView
這聽起來很簡單的文本,但在這裏不斷拋出的錯誤是我的功能:TextView.setText()拋出NullPointerException異常
private void getAndPutNum(){
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String myNum = tm.getLine1Number();
TextView numDisplay = (TextView)findViewById(R.id.myNum);
try{
numDisplay.setText(myNum);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
我在我的「用途設置READS_PHONE_STATE
許可'我的Manifest。任何人都可以看到我可能做錯了什麼?
UPDATE:
這裏是我的佈局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TextView android:gravity="center|center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/number" android:textSize="65px" android:layout_gravity="center|center_vertical|center_horizontal" android:layout_height="fill_parent" android:layout_weight=".8" android:layout_width="fill_parent" android:layout_marginTop="-40dp" android:keepScreenOn="true" android:id="@+id/myNum"></TextView>
請張貼您的佈局xml – MByD