如何在Android片段中创建新的类对象?

时间:2014-03-12 20:20:34

标签: android button dynamic fragment

我想通过将SherlockFragment扩展到我的父类来创建一个Button类对象,但是我遇到了几个错误,我不明白。

我想使用Button类对象创建动态按钮。

Java代码 - :

 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代码 - :

<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>

错误 - :

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 to instantiate activity ComponentInfo{com.example.viewproblem/com.example.viewproblem.MainActivity}:   java.lang.ClassCastException: com.example.viewproblem.MainActivity cannot be cast to  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):at java.lang.reflect.Method.invokeNative(Native Method)    03-24 08:59:54.573:    E / AndroidRuntime(18731):at java.lang.reflect.Method.invoke(Method.java:511)    03-24 08:59:54.573:    E / AndroidRuntime(18731):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)    03-24 08:59:54.573:    E / AndroidRuntime(18731):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)    03-24 08:59:54.573:    E / AndroidRuntime(18731):at dalvik.system.NativeStart.main(Native Method)    03-24 08:59:54.573:    E / AndroidRuntime(18731):引起:java.lang.ClassCastException:com.example.viewproblem.MainActivity无法强制转换为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更多

这是我上面的代码,我无法找到正确的错误。请让我知道,我的代码有一些很好的解决方案。

0 个答案:

没有答案