button - how to create a new class object in Android fragment? -


i want create button class object extending sherlockfragment parent class facing several errors , don't understand.

i want create dynamic buttons using button class object.

java code-:

 package com.example.viewproblem;   import com.actionbarsherlock.app.sherlockfragment;  import android.app.activity;  import android.os.bundle;  import android.view.layoutinflater;  import android.view.view;  import android.view.viewgroup;  import android.view.viewgroup.layoutparams;  import android.widget.button;  import android.widget.linearlayout;    public class mainactivity extends sherlockfragment   {     private static activity activity;     static linearlayout lm;       @override     public void onactivitycreated(bundle savedinstancestate)      {         super.onactivitycreated(savedinstancestate);       }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate)      {           view rootview1 = inflater.inflate(r.layout.activity_main, container, false);          lm = (linearlayout) rootview1.findviewbyid(r.id.linearmain);          linearlayout lla = new linearlayout(activity.getapplicationcontext());          linearlayout.layoutparams params = new linearlayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content);          button  btn = new button(activity.getapplicationcontext());          btn.setid(3);          btn.settext("hello");          btn.setlayoutparams(params);          lla.addview(btn);          lm.addview(lla);           return rootview1;       }   } 

xml code-:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearmain" android:background="#ffffff" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".firstscreen"  android:orientation="vertical">   </linearlayout> 

error-:

03-24 08:59:54.573:  e/androidruntime(18731): fatal exception: main 03-24 08:59:54.573: e/androidruntime(18731): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.viewproblem/com.example.viewproblem.mainactivity}:   java.lang.classcastexception: com.example.viewproblem.mainactivity cannot cast  android.app.activity 

03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread.performlaunchactivity(activitythread.java:1880) 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread.access$600(activitythread.java:123) 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread$h.handlemessage(activitythread.java:1147) 03-24 08:59:54.573: e/androidruntime(18731): @ android.os.handler.dispatchmessage(handler.java:99) 03-24 08:59:54.573: e/androidruntime(18731): @ android.os.looper.loop(looper.java:137) 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread.main(activitythread.java:4424) 03-24 08:59:54.573: e/androidruntime(18731): @ java.lang.reflect.method.invokenative(native method) 03-24 08:59:54.573: e/androidruntime(18731): @ java.lang.reflect.method.invoke(method.java:511) 03-24 08:59:54.573: e/androidruntime(18731): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 03-24 08:59:54.573: e/androidruntime(18731): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 03-24 08:59:54.573: e/androidruntime(18731): @ dalvik.system.nativestart.main(native method) 03-24 08:59:54.573: e/androidruntime(18731): caused by: java.lang.classcastexception: com.example.viewproblem.mainactivity cannot cast android.app.activity 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.instrumentation.newactivity(instrumentation.java:1023) 03-24 08:59:54.573: e/androidruntime(18731): @ android.app.activitythread.performlaunchactivity(activitythread.java:1871) 03-24 08:59:54.573: e/androidruntime(18731): ... 11 more

this above code , not able find out proper error. please let me know , solution code.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -