在布局之间切换:onclicklistener使应用程序崩溃

时间:2014-07-20 06:54:02

标签: android android-layout android-linearlayout

我是Android开发的新手。当我使用下面的代码时,我的应用程序崩溃,如果我删除onclicklistner,它的工作完美 这是代码..

   int[] vmarray= {R.drawable.vm1bulb, R.drawable.vm2chair, R.drawable.vm3comb,
   R.drawable.vm4cycle,R.drawable.vm5dairy,R.drawable.vm6fan,R.drawable.vm7mobile,
   R.drawable.vm8pen,R.drawable.vm9shoes,R.drawable.vm10toothbrush,  
   R.drawable.vm11bangle, R.drawable.vm12watch};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
     // TODO Auto-generated method stub

     super.onCreate(savedInstanceState);


    inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

     setContentView(R.layout.cacrvisualmem);

      firstview =(LinearLayout)this.findViewById(R.id.firsthumlayout);

    secondview = (LinearLayout) inflater.inflate(R.layout.cacrvisualmempart1, null);

   for(int i=0;i<6;i++)
     { 
     final int a = i;
     button_var[a] = (Button)findViewById(idArray[a]); 
     }

//在第一个布局中,有6个按钮,其背景是随机设置的   //来自ARRAY的抽签&#34; vmarray&#34;。以下代码适用于

     Random randomGenerator = new Random();
      Random rand6 = new Random();

     while (numbers.size() < 6) {
     random1 = randomGenerator.nextInt(12);
     random2 = rand6.nextInt(6);

      if (!numbers.contains(random1) && (!numbers2.contains(random2))) 
       {
           numbers.add(random1);

           numbers2.add(random2);  

           b[random2].setBackgroundResource(vmarray[random1]);
           count++;         

        }//if ends
     }//while ends

     Handler changeview = new Handler();

     Runnable r1 = new Runnable(){
        @Override
        public void run() {
            // TODO Auto-generated method stub

     // NOW, here, after 1 minute, second view will be inflated and button1  
     //  background is set to one the image from the array

            setContentView(secondview);

            b11=(Button)findViewById(R.id.button1);

            queryrandvar=queryrand.nextInt(12);

            switch(queryrandvar)
            {
            case 0:
                b11.setBackgroundResource(vmarray[0]);
                queryval=0;
                break;
            case 1:
                b11.setBackgroundResource(vmarray[1]);
                queryval=1;
                break;
            case 2:
                b11.setBackgroundResource(vmarray[2]);
                queryval=2;
                break;
            case 3:
                b11.setBackgroundResource(vmarray[3]);
                queryval=3;
                break;
            case 4:
                b11.setBackgroundResource(vmarray[4]);
                queryval=4;
                break;
            case 5:
                b11.setBackgroundResource(vmarray[5]);
                queryval= 5;
                break;
            case 6:
                b11.setBackgroundResource(vmarray[6]);
                queryval= 6;
                break;
            case 7:
                b11.setBackgroundResource(vmarray[7]);
                queryval= 7;
                break;
            case 8:
                b11.setBackgroundResource(vmarray[8]);
                queryval= 8;
                break;
            case 9:
                b11.setBackgroundResource(vmarray[9]);
                queryval= 9;
                break;
            case 10:
                b11.setBackgroundResource(vmarray[10]);
                queryval= 10;
                break;
            case 11:
                b11.setBackgroundResource(vmarray[11]);
                queryval= 11;
                break;

        }//switch ends


        }//run ends
     };//runnable ends

     b11.setOnClickListener(null);

     changeview.postDelayed(r1,10000);

  } //on create method ends here



    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1:
      {

       b11.setBackgroundResource(vmarray[1]);

       break;
      }




     }//switch ends
  }//onclick ends

如果我删除onclicklistner,它运行得很好,但是实现它会崩溃应用程序,我的onclick方法或onclick listner出了什么问题

   LOGCAT IS AS FOLLOW




    07-20 03:06:15.980: E/AndroidRuntime(798): FATAL EXCEPTION: main
07-20 03:06:15.980: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.univ.cog/com.univ.cog.Ca}: java.lang.NullPointerException
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:137)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:5103)
07-20 03:06:15.980: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
07-20 03:06:15.980: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:525)
07-20 03:06:15.980: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-20 03:06:15.980: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-20 03:06:15.980: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
07-20 03:06:15.980: E/AndroidRuntime(798): Caused by: java.lang.NullPointerException
07-20 03:06:15.980: E/AndroidRuntime(798):  at com.univ.cog.Ca.onCreate(Ca.java:162)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.Activity.performCreate(Activity.java:5133)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-20 03:06:15.980: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-20 03:06:15.980: E/AndroidRuntime(798):  ... 11 more
07-20 03:06:16.040: W/ActivityManager(285):   Force finishing activity com.univ.cog/.Ca

2 个答案:

答案 0 :(得分:0)

这里你传递了null

b11.setOnClickListener(null);


@Override
public void onClick(View v) {

    //here you getting v with value null, v.getId() throw NullPointerException

    switch(v.getId()){

        case R.id.button1:{

            b11.setBackgroundResource(vmarray[1]);

            break;
        }                                           

    }//switch ends
}//onclick ends

<强>编辑:

如果您接受更改布局更改方式的建议,请使用RelativeLayout,LinearLayout重叠2,然后控制可见性。

public class MyActivity extends Activity {

    LinearLayout myContent1;
    LinearLayout myContent2;

    @Override
    public View onCreate( Bundle savedInstanceState ) {

        this.myContent1 = ( LinearLayout ) view.findViewById( R.id.content1 );
        this.myContent2 = ( LinearLayout ) view.findViewById( R.id.content1 );

        if ( /*some condition*/) {
            this.myContent1.setVisibility( View.VISIBLE );
        } else {
            this.myContent2.setVisibility( View.GONE );
        }
    }
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/content1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/content2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone" >

        </LinearLayout>

</RelativeLayout>

再次编辑:尝试此操作以查看您的Button是否为空

b11=(Button)findViewById(R.id.button1)

if(b11 != null){

   b11.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
    });

}else{

   System.out.println("Button is null");
}

答案 1 :(得分:0)

我知道了......在RUNNABLE内部切换声明后,我已经将以下内容排除在外。工作精细,没有任何APP崩溃。

                b11.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                b11.setBackgroundResource(vmarray[0]);//JUST TO CHECK
                }
             });