我正在尝试在onCreate之后对方法中的视图(XML布局文件)进行充气和添加。父级是一个放置在FRAGMENT布局中的LinearLayout,我通过findView视图访问findViewById。但是我不能让视图膨胀/添加(它必须动态添加,因为用户添加了这些视图)。
我onCreate的片段:
CallFragment callFragment = (CallFragment) tabsPagerAdapter.getItem(1);
callRootView = callFragment.getView();
mainLayout = (LinearLayout)callRootView.findViewById(R.id.call_main_layout);
我的CreateCall方法的片段:
private void CreateCall()
{
//try{
LayoutInflater i = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout c = (LinearLayout)i.inflate(R.layout.call_display, null);
//c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 50));
mainLayout.addView(c);
Log.i("Success", "");
/*}catch (Exception e) {
Log.i("ERROR", e.toString());
}*/
}
我的LogCat:
06-26 09:48:08.644 25001-25001/com.voytech.stockcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.voytech.stockcall, PID: 25001
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.voytech.stockcall/com.voytech.stockcall.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.voytech.stockcall.MainActivity.onCreate(MainActivity.java:148)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
MY SECOND APPROACH: 这段代码都在我的片段里面,仍然不起作用。从我的MainActivity访问该方法,希望这不是问题。 onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.call_fragment, container, false);
mainLayout = (LinearLayout) rootView.findViewById(R.id.call_main_layout); //mainLayout references our direct LinearLayout(the container) in our XML document, this is also the parent that we will add the child view to
//Reference inflater and container if needed
layoutInflater = inflater;
layoutContainer = container;
}
这是 AddCallLayout 方法,它通过片段TabsPagerAdapter从我们的MainActivity中调用,我删除了一些注释掉的代码,因为这只是我尝试过的多种方法之一:
public void AddCallLayout()
{
callDisplay = layoutInflater.inflate(R.layout.call_display, layoutContainer, false);
mainLayout.addView(callDisplay);
}
Fragment LogCat:
06-26 10:38:13.840 14354-14354/com.voytech.stockcall E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.voytech.stockcall, PID: 14354
java.lang.NullPointerException
at com.voytech.stockcall.CallFragment.AddCallLayout(CallFragment.java:146)
at com.voytech.stockcall.MainActivity$2.onClick(MainActivity.java:675)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)