在JAVA ANDROID SDK中动态创建按钮和文本

时间:2014-04-17 20:05:31

标签: java android android-layout android-intent android-listview

我在ANDROID SDK中使用以下简单代码时出错:

LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);

Button button1 = new Button(this);
button1.setText(R.string.button_misto);

TextView textView1 = new TextView(this);
textView1.setText("Something cool");

layout1.addView(textView1);

我想做的只是在运行时创建对象。

我在这一行得到java.lang.NullPointerException

layout1.addView(textView1);

layout1.addView(button1);

这就像button1textView1NULL。我做错了什么?

这是错误:

04-17 22:52:18.046: E/FragmentManager(23649): No view found for id 0x7f05003c (com.example.app2:id/container) for fragment PlaceholderFragment{4057ebf8 #0 id=0x7f05003c}
04-17 22:52:18.046: E/FragmentManager(23649): Activity state:
04-17 22:52:18.046: D/FragmentManager(23649):   Local FragmentActivity 40574780 State:
04-17 22:52:18.046: D/FragmentManager(23649):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
04-17 22:52:18.046: D/FragmentManager(23649):     mLoadersStarted=false
04-17 22:52:18.046: D/FragmentManager(23649):   Active Fragments in 405748c8:
04-17 22:52:18.046: D/FragmentManager(23649):     #0: PlaceholderFragment{4057ebf8 #0 id=0x7f05003c}
04-17 22:52:18.046: D/FragmentManager(23649):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
04-17 22:52:18.054: D/FragmentManager(23649):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
04-17 22:52:18.054: D/FragmentManager(23649):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
04-17 22:52:18.054: D/FragmentManager(23649):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
04-17 22:52:18.054: D/FragmentManager(23649):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
04-17 22:52:18.062: D/FragmentManager(23649):       mFragmentManager=FragmentManager{405748c8 in ForthActivity{40574780}}
04-17 22:52:18.062: D/FragmentManager(23649):       mActivity=com.example.app2.ForthActivity@40574780
04-17 22:52:18.062: D/FragmentManager(23649):   Added Fragments:
04-17 22:52:18.062: D/FragmentManager(23649):     #0: PlaceholderFragment{4057ebf8 #0 id=0x7f05003c}
04-17 22:52:18.062: D/FragmentManager(23649):   FragmentManager misc state:
04-17 22:52:18.062: D/FragmentManager(23649):     mActivity=com.example.app2.ForthActivity@40574780
04-17 22:52:18.062: D/FragmentManager(23649):     mContainer=android.support.v4.app.FragmentActivity$2@40574940
04-17 22:52:18.062: D/FragmentManager(23649):     mCurState=2 mStateSaved=false mDestroyed=false
04-17 22:52:18.070: D/FragmentManager(23649):   View Hierarchy:
04-17 22:52:18.070: D/FragmentManager(23649):     com.android.internal.policy.impl.PhoneWindow$DecorView{405755e0 V.E..... ... 0,0-0,0}
04-17 22:52:18.070: D/FragmentManager(23649):       android.widget.FrameLayout{405759b8 V.E..... ... 0,0-0,0}
04-17 22:52:18.070: D/FragmentManager(23649):         android.widget.LinearLayout{405762f0 V.E..... ... 0,0-0,0}
04-17 22:52:18.070: D/FragmentManager(23649):           android.support.v7.internal.widget.ActionBarContainer{40576710 V.ED.... ... 0,0-0,0 #7f05001b app:id/action_bar_container}
04-17 22:52:18.070: D/FragmentManager(23649):             android.support.v7.internal.widget.ActionBarView{40576e70 V.E..... ... 0,0-0,0 #7f05001c app:id/action_bar}
04-17 22:52:18.070: D/FragmentManager(23649):               android.widget.LinearLayout{4057b168 G.....C. ... 0,0-0,0}
04-17 22:52:18.070: D/FragmentManager(23649):                 android.widget.ImageView{4057bab8 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
04-17 22:52:18.078: D/FragmentManager(23649):                 android.widget.LinearLayout{4057bcf0 V.E..... ... 0,0-0,0}
04-17 22:52:18.078: D/FragmentManager(23649):                   android.widget.TextView{4057bf58 V.ED.... ... 0,0-0,0 #7f050022 app:id/action_bar_title}
04-17 22:52:18.078: D/FragmentManager(23649):                   android.widget.TextView{4057cb88 G.ED.... ... 0,0-0,0 #7f050023 app:id/action_bar_subtitle}
04-17 22:52:18.078: D/FragmentManager(23649):               android.support.v7.internal.widget.ActionBarView$HomeView{40577fa8 VFE...C. ... 0,0-0,0}
04-17 22:52:18.078: D/FragmentManager(23649):                 android.widget.ImageView{40579d18 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
04-17 22:52:18.078: D/FragmentManager(23649):                 android.widget.ImageView{4057a0e0 V.ED.... ... 0,0-0,0 #7f050014 app:id/home}
04-17 22:52:18.078: D/FragmentManager(23649):             android.support.v7.internal.widget.ActionBarContextView{4057d170 G.E..... ... 0,0-0,0 #7f05001d app:id/action_context_bar}
04-17 22:52:18.078: D/FragmentManager(23649):           android.widget.FrameLayout{4057d840 V.E..... ... 0,0-0,0 #1020002 android:id/content}
04-17 22:52:18.085: D/FragmentManager(23649):             android.widget.LinearLayout{4057ed38 V.E..... ... 0,0-0,0}
04-17 22:52:18.085: D/FragmentManager(23649):               android.widget.TextView{4057efe0 V.ED.... ... 0,0-0,0}
04-17 22:52:18.085: D/FragmentManager(23649):           android.support.v7.internal.widget.ActionBarContainer{4057daa8 G.ED.... ... 0,0-0,0 #7f05001e app:id/split_action_bar}
04-17 22:52:18.085: D/AndroidRuntime(23649): Shutting down VM
04-17 22:52:18.085: W/dalvikvm(23649): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-17 22:52:18.093: E/AndroidRuntime(23649): FATAL EXCEPTION: main
04-17 22:52:18.093: E/AndroidRuntime(23649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app2/com.example.app2.ForthActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.app2:id/container) for fragment PlaceholderFragment{4057ebf8 #0 id=0x7f05003c}
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.os.Looper.loop(Looper.java:130)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at dalvik.system.NativeStart.main(Native Method)
04-17 22:52:18.093: E/AndroidRuntime(23649): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.app2:id/container) for fragment PlaceholderFragment{4057ebf8 #0 id=0x7f05003c}
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.Activity.performStart(Activity.java:3802)
04-17 22:52:18.093: E/AndroidRuntime(23649):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
04-17 22:52:18.093: E/AndroidRuntime(23649):    ... 11 more
04-17 22:53:37.367: W/KeyCharacterMap(23700): Can't open keycharmap file
04-17 22:53:37.367: W/KeyCharacterMap(23700): Error loading keycharmap file
04-17 22:53:37.367: W/KeyCharacterMap(23700): Using default keymap
04-17 22:53:52.750: W/KeyCharacterMap(23771): Can't open keycharmap file
04-17 22:53:52.750: W/KeyCharacterMap(23771): Error loading keycharmap file
04-17 22:53:52.750: W/KeyCharacterMap(23771): Using default keymap
04-17 22:55:58.898: W/KeyCharacterMap(23953): Can't open keycharmap file
04-17 22:55:58.898: W/KeyCharacterMap(23953): Error loading keycharmap file
04-17 22:55:58.898: W/KeyCharacterMap(23953): Using default keymap
04-17 22:56:00.164: D/dalvikvm(23953): GC_CONCURRENT freed 233K, 46% free 3170K/5831K, external 922K/1036K, paused 5ms+5ms
04-17 22:56:00.195: D/dalvikvm(23953): GC_FOR_MALLOC freed 54K, 47% free 3180K/5959K, external 922K/1036K, paused 29ms
04-17 22:56:00.234: D/dalvikvm(23953): GC_FOR_MALLOC freed 3K, 47% free 3305K/6151K, external 922K/1036K, paused 28ms
04-17 22:56:00.281: D/dalvikvm(23953): GC_FOR_MALLOC freed 7K, 47% free 3297K/6151K, external 922K/1036K, paused 32ms
04-17 22:56:00.328: D/dalvikvm(23953): GC_FOR_MALLOC freed 0K, 44% free 3809K/6727K, external 922K/1036K, paused 28ms
04-17 22:56:00.445: D/dalvikvm(23953): GC_CONCURRENT freed 2K, 44% free 3822K/6727K, external 922K/1036K, paused 4ms+3ms
04-17 22:56:01.242: W/KeyCharacterMap(23953): Can't open keycharmap file
04-17 22:56:01.242: W/KeyCharacterMap(23953): Error loading keycharmap file
04-17 22:56:01.242: W/KeyCharacterMap(23953): Using default keymap
04-17 22:56:28.757: D/dalvikvm(24107): GC_FOR_MALLOC freed 157K, 45% free 3227K/5767K, external 922K/1036K, paused 93ms
04-17 22:56:28.859: D/dalvikvm(24107): GC_FOR_MALLOC freed 46K, 45% free 3308K/5959K, external 922K/1036K, paused 96ms
04-17 22:56:29.007: D/dalvikvm(24107): GC_FOR_MALLOC freed 3K, 42% free 3817K/6535K, external 922K/1036K, paused 126ms
04-17 22:56:29.132: D/dalvikvm(24107): GC_CONCURRENT freed 18K, 42% free 3807K/6535K, external 922K/1036K, paused 4ms+4ms
04-17 22:56:29.664: W/KeyCharacterMap(24107): Can't open keycharmap file
04-17 22:56:29.664: W/KeyCharacterMap(24107): Error loading keycharmap file
04-17 22:56:29.664: W/KeyCharacterMap(24107): Using default keymap
04-17 22:56:30.757: D/AndroidRuntime(24107): Shutting down VM
04-17 22:56:30.757: W/dalvikvm(24107): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-17 22:56:30.765: E/AndroidRuntime(24107): FATAL EXCEPTION: main
04-17 22:56:30.765: E/AndroidRuntime(24107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app2/com.example.app2.ForthActivity}: java.lang.NullPointerException
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.os.Looper.loop(Looper.java:130)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at dalvik.system.NativeStart.main(Native Method)
04-17 22:56:30.765: E/AndroidRuntime(24107): Caused by: java.lang.NullPointerException
04-17 22:56:30.765: E/AndroidRuntime(24107):    at com.example.app2.ForthActivity.onCreate(ForthActivity.java:40)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 22:56:30.765: E/AndroidRuntime(24107):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-17 22:56:30.765: E/AndroidRuntime(24107):    ... 11 more

修改

ForthActivity.java http://pastebin.com/TDbHikXZ

activity_forth.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.app2.ForthActivity"
    tools:ignore="MergeRootFrame" />

fragment_forth.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/layout1"
    >

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

layout1为null,可能您没有使用setContentView行,或者您的布局中不存在此视图。

答案 1 :(得分:0)

这是因为您要将内容视图设置为布局activity_forth.xml 然后你通过id(R.id.layout1)调用查找视图,该视图不在此布局中。 您正在搜索当前布局中不存在的视图。

这应该让你开始 setContentView(R.layout.fragment_forth.xml); LinearLayout ll = () findViewById(R.id.layout1);

希望它有所帮助。