2015-03-02 45 views
0

我想添加一個textview,並在嘗試將其添加到佈局時不斷獲取nullPointer。無法添加一個TextView programmaticaly

我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/app_header" android:layout_width="match_parent" 
    android:layout_height="@dimen/headerSize" android:background="@color/iPhoneBackground"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@color/grey" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

我的類:

public class AppHeader extends RelativeLayout { 
    Context mContext; 
    screenName sName; 
    RelativeLayout parent; 


    public AppHeader(Context context, screenName sn) { 
     super(context); 
     mContext = context; 
     sName = sn; 
     parent = (RelativeLayout) findViewById(R.id.app_header); 
     init(); 
    } 

    public AppHeader(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     mContext = context; 
     init(); 
    } 
    public AppHeader(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
     init(); 
    } 

    public void init(){ 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     TextView title = new TextView(mContext); 
     switch (sName) { 
      case FAVORITER: 
       title.setText(mContext.getString(R.string.favourite)); 
       break; 
      case SOKRESA: 
       title.setText(mContext.getString(R.string.search)); 
       break; 
      case REALTID: 
       // tod 
       break; 
      case STORNINGAR: 
       // tod 
       break; 
      case MER: 
       // tod 
       break; 
     } 

     params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
     title.setLayoutParams(params); 
     parent.addView(title); 
    } 

} 

logcat的給我上最後一行空指針 「parent.addView(稱號);」

有誰知道我在做什麼錯?

這裏是logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{mtr.se.reskoll/mtr.se.reskoll.SokResa}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
      at android.app.ActivityThread.access$600(ActivityThread.java:141) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5041) 
      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:793) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at mtr.se.reskoll.AppHeader.init(AppHeader.java:63) 
      at mtr.se.reskoll.AppHeader.<init>(AppHeader.java:24) 
      at mtr.se.reskoll.SokResa.setHeader(SokResa.java:50) 
      at mtr.se.reskoll.SokResa.onCreate(SokResa.java:37) 
      at android.app.Activity.performCreate(Activity.java:5104) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5041) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
+0

發佈您的logcat – Apurva 2015-03-02 16:26:32

回答

1

你 「parent」 變量是在構造函數中public AppHeader(Context context, screenName sn)僅初始化。如果您使用其他構造函數,則parent將爲空。

該行parent = (RelativeLayout) findViewById(R.id.app_header);必須在您的init()方法中移動。

0

試試這個:

Activity activity = (Activity) mContext; 
parent = (RelativeLayout) activity.findViewById(R.id.app_header); 
+0

雖然此代碼塊可能會回答問題,請嘗試在代碼中添加相關信息以改善您的答案。看到http://stackoverflow.com/help/how-to-answer – SMR 2015-03-03 08:42:40

0
public AppHeader(Context context) { 
     super(context); 
     mContext = context; 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     parent = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this); 

     init(); 
    } 

    public AppHeader(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     mContext = context; 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     parent = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this); 

     init(); 
    } 
    public AppHeader(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
     parent = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this); 

     init(); 
    } 

編輯:對於文本顯示,這取決於您使用的構造,因爲你只設置在第一個構造函數的文本。創建因此後您的自定義視圖(我還修改了第一個構造函數)

public void SetTitle(sName) 
{ 
     switch (sName) { 
      case FAVORITER: 
       title.setText(mContext.getString(R.string.favourite)); 
       break; 
      case SOKRESA: 
       title.setText(mContext.getString(R.string.search)); 
       break; 
      case REALTID: 
       // tod 
       break; 
      case STORNINGAR: 
       // tod 
       break; 
      case MER: 
       // tod 
       break; 
     } 
} 

public void init(){ 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     TextView title = new TextView(mContext); 

     // we will add this line for test, to see in the problem is that the text is not displayed or the textview itself is not visible 
     title.setBackgroundColor(Color.YELLOW); 

     params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
     title.setLayoutParams(params); 
     parent.addView(title); 
    } 

AppHeader header= new AppHeader (context); 
header.SetTitle("Test"); 

而且,只是爲了確保開關

我會建議不同的方法正確工作(又名u正在發送一個實際包含在開關選項中的屏幕名稱),請嘗試一種簡單的測試方法,而不是上述方法:

public void SetTitle(sName) 
{ 
    title.setText("Test Title"); 
} 
+0

這個答案擺脫了我的空指針,但似乎沒有添加到佈局的textview ...它仍然是空白 – Jimbo 2015-03-02 16:50:22

+0

@Jimbo,我編輯了我的答案,如果有幫助,請將其標爲正確。謝謝 :) – 2015-03-03 08:02:45