我想添加一個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)
發佈您的logcat – Apurva 2015-03-02 16:26:32