0

我正在尝试在该类的另一个类中创建一个动态按钮,Penicillins.class但出现此错误

私人最终活动 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);

这是具有创建新按钮的按钮的java:

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

        }}

记录猫错误

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

也堆叠

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]

我认为问题可能是我的目录因剪切粘贴一些东西而变得很糟糕,因为 sdk 管理器现在告诉我我没有安装所有正确的平台工具,现在下载它们。

4

1 回答 1

0

编辑:它不起作用的原因是你必须将你的 RelativeLayout 声明为一个全局变量(这是你应该知道的基本编程),然后在你 setContentView 之后在你的 onCreate() 中给它一个值。像这样:

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); 
        }
于 2013-10-28T04:14:59.767 回答