简体   繁体   中英

dynamic button in different class

I am trying to create a dynamic button in another class from this class Penicillins.class but I am getting this error

private final Activity performLaunchActivity(ActivityRecord r) {

        // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");

        ActivityInfo aInfo = r.activityInfo;
        if (r.packageInfo == null) {
            r.packageInfo = getPackageInfo(aInfo.applicationInfo,
                    Context.CONTEXT_INCLUDE_CODE);
        }

        ComponentName component = r.intent.getComponent();
        if (component == null) {
            component = r.intent.resolveActivity(
                mInitialApplication.getPackageManager());
            r.intent.setComponent(component);
        }

        if (r.activityInfo.targetActivity != null) {
            component = new ComponentName(r.activityInfo.packageName,
                    r.activityInfo.targetActivity);
        }

        Activity activity = null;
        try {
            java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
            activity = mInstrumentation.newActivity(
                    cl, component.getClassName(), r.intent);
            r.intent.setExtrasClassLoader(cl);
            if (r.state != null) {
            r.state.setClassLoader(cl);

this is the java that has the button that creates the new button:

 package org.iimed.www;

    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;

    import org.iimed.www.Sundayopen;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;

    import android.graphics.drawable.Drawable;

    public class Penicillins extends Activity implements OnClickListener{

        ImageButton back,addmed;
        Context adaba;
        Drawable mmimed;
        Button button;
        RelativeLayout rl;

        public void onCreate(Bundle SavedInstanceState){
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.penicillin);
             rl = (RelativeLayout)findViewById(R.id.sundayopen);
         back = (ImageButton) findViewById(R.id.back);
         addmed = (ImageButton) findViewById(R.id.addmed);
         back.setOnClickListener(this);
         addmed.setOnClickListener(this); 
        }


        ImageButton addmed = (ImageButton) findViewById(R.id.addmed);
        public void onClick(View v) {



         LayoutParams b = new LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                     LinearLayout.LayoutParams.WRAP_CONTENT);

           Button button = new Button(this);
           button.setText("hello");
           button.setLayoutParams(b);
           rl.addView(button);

        }}

log cat errors

01-08 22:10:52.505: E/AndroidRuntime(14353): FATAL EXCEPTION: main
01-08 22:10:52.505: E/AndroidRuntime(14353): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4568)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.ActivityThread.access$1400(ActivityThread.java:152)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.os.Looper.loop(Looper.java:137)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.ActivityThread.main(ActivityThread.java:5328)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at dalvik.system.NativeStart.main(Native Method)
01-08 22:10:52.505: E/AndroidRuntime(14353): Caused by: java.lang.NullPointerException
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:387)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.LoadedApk.getClassLoader(LoadedApk.java:325)
01-08 22:10:52.505: E/AndroidRuntime(14353):    at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
01-08 22:10:52.505: E/AndroidRuntime(14353):    ... 11 more

also stack

ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2173    
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2297 
ActivityThread.access$700(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 152    
ActivityThread$H.handleMessage(Message) line: 1282  
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 5328    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 511  
ZygoteInit$MethodAndArgsCaller.run() line: 1102 
ZygoteInit.main(String[]) line: 869 
NativeStart.main(String[]) line: not available [native method]

I think the problem may be my directories are all screwy from cut pasting some things around, as the sdk manager now tells me I don't have all the correct platform tools installed, downloading them now.

EDIT: The reason it's not working is you have to declare your RelativeLayout as a global variable (this is basic programming you should know this) and then give it a value in your onCreate() after you setContentView. Like this:

public class Penicillins extends Activity implements OnClickListener{

        ImageButton back,addmed;
        Context adaba;
        Drawable mmimed;
        Button button;
        RelativeLayout rl;

        public void onCreate(Bundle SavedInstanceState){
         super.onCreate(SavedInstanceState);
         setContentView(R.layout.penicillin);
         rl = (RelativeLayout)findViewById(R.id.sundayopen);
         back = (ImageButton) findViewById(R.id.back);
         addmed = (ImageButton) findViewById(R.id.addmed);
         back.setOnClickListener(this);
         addmed.setOnClickListener(this); 
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM