Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OnClickListener的多个赋值问题_Java_Android - Fatal编程技术网

Java OnClickListener的多个赋值问题

Java OnClickListener的多个赋值问题,java,android,Java,Android,我在使用OnClickListener的多个赋值时遇到问题。如果我对同一个侦听器使用3个不同的按钮,我的程序运行平稳,但当我使用4个按钮时,我的应用程序强制关闭。我甚至创建了自己的OnClickListener,并将其分配给我的按钮,但它仍然强制关闭 mymain屏幕 package com.example.wifisurveillance; import android.os.Bundle; import android.app.Activity; import android.view.M

我在使用
OnClickListener
的多个赋值时遇到问题。如果我对同一个侦听器使用3个不同的按钮,我的程序运行平稳,但当我使用4个按钮时,我的应用程序强制关闭。我甚至创建了自己的
OnClickListener
,并将其分配给我的按钮,但它仍然强制关闭

mymain屏幕

package com.example.wifisurveillance;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.net.NetworkInfo;
import android.net.ConnectivityManager;
import android.content.Context;
import android.util.Log;
import android.net.Uri;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.database.Cursor;
import android.provider.MediaStore;


public class myMainScreen  extends Activity implements OnClickListener{
boolean isConnected;
private boolean checkWifi() {
    ConnectivityManager connManager =
                      (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnected =  mWifi.isConnected();
    return isConnected;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Button buttonA = (Button) findViewById(R.id.button1);    
    Button buttonB = (Button) findViewById(R.id.button2); 
    Button buttonD = (Button) findViewById(R.id.button4);
    Button buttonF = (Button) findViewById(R.id.button6);
    buttonA.setOnClickListener(this);
    buttonB.setOnClickListener(this);
    buttonD.setOnClickListener(this);
    buttonF.setOnClickListener(this);
}


        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
            case R.id.button1:
            isConnected =  checkWifi();
            Log.d("mylog", "Wifi State =" + isConnected);

            if (isConnected) {
            Intent intent = new Intent(myMainScreen.this, WifiPage.class);
            startActivity(intent);
            }
            else
            {
                openWifiSettings();
            }
            break;

            case R.id.button2:
             Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
             final int ACTIVITY_SELECT_IMAGE = 1234;
             startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

             break;
            case R.id.button4:
                Intent intent = new Intent(myMainScreen.this, Sensor_log.class);
                startActivity(intent);
                break;

            case R.id.button6:
                finish();
                System.exit(0);

                break;

            }
        }






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;


}
public void openWifiSettings(){

    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity( intent);
 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) { 
    case 1234:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
        }
    }

};

 }
Xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifisurveillance"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="18" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".WifiPage"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.SURVEILLANCEPAGE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".myMainScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Sensor_log"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.SENSORPAGE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


</application>

</manifest>

航海日志

12-26 18:03:42.040: E/Trace(24982): error opening trace file: No such file or directory (2)
12-26 18:03:42.280: D/dalvikvm(24982): GC_FOR_ALLOC freed 70K, 8% free 9384K/10115K, paused 31ms, total 31ms
12-26 18:03:42.290: I/dalvikvm-heap(24982): Grow heap (frag case) to 10.183MB for 466576-byte allocation
12-26 18:03:42.330: D/dalvikvm(24982): GC_CONCURRENT freed 1K, 8% free 9839K/10631K, paused 14ms+2ms, total 39ms
12-26 18:03:42.330: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 15ms
12-26 18:03:42.350: D/dalvikvm(24982): GC_FOR_ALLOC freed 0K, 8% free 9839K/10631K, paused 23ms, total 23ms
12-26 18:03:42.350: I/dalvikvm-heap(24982): Grow heap (frag case) to 11.183MB for 1049776-byte allocation
12-26 18:03:42.400: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 8% free 10864K/11719K, paused 18ms+3ms, total 47ms
12-26 18:03:42.400: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 19ms
12-26 18:03:42.420: D/InputTransport(24982): Input channel constructed: name='421455b8 com.example.wifisurveillance/com.example.wifisurveillance.MainActivity (client)', fd=41
12-26 18:03:42.550: D/libEGL(24982): loaded /system/lib/egl/libEGL_mali.so
12-26 18:03:42.560: D/libEGL(24982): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-26 18:03:42.570: D/libEGL(24982): loaded /system/lib/egl/libGLESv2_mali.so
12-26 18:03:42.600: D/OpenGLRenderer(24982): Enabling debug mode 0
12-26 18:03:47.545: D/dalvikvm(24982): GC_FOR_ALLOC freed 471K, 11% free 10474K/11719K, paused 17ms, total 17ms
12-26 18:03:47.545: I/dalvikvm-heap(24982): Grow heap (frag case) to 11.388MB for 614416-byte allocation
12-26 18:03:47.575: D/dalvikvm(24982): GC_CONCURRENT freed 2K, 11% free 11072K/12359K, paused 13ms+1ms, total 36ms
12-26 18:03:47.575: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 13ms
12-26 18:03:47.605: D/dalvikvm(24982): GC_FOR_ALLOC freed 0K, 11% free 11072K/12359K, paused 20ms, total 20ms
12-26 18:03:47.605: I/dalvikvm-heap(24982): Grow heap (frag case) to 12.705MB for 1382416-byte allocation
12-26 18:03:47.645: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 10% free 12422K/13767K, paused 15ms+2ms, total 37ms
12-26 18:03:47.645: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 5ms
12-26 18:03:47.675: D/dalvikvm(24982): GC_FOR_ALLOC freed 600K, 15% free 11829K/13767K, paused 17ms, total 17ms
12-26 18:03:47.685: I/dalvikvm-heap(24982): Grow heap (frag case) to 13.125MB for 1048592-byte allocation
12-26 18:03:47.735: D/dalvikvm(24982): GC_CONCURRENT freed <1K, 14% free 12853K/14855K, paused 30ms+2ms, total 57ms
12-26 18:03:47.795: D/dalvikvm(24982): GC_FOR_ALLOC freed 0K, 14% free 12853K/14855K, paused 23ms, total 23ms
12-26 18:03:47.815: I/dalvikvm-heap(24982): Grow heap (frag case) to 15.375MB for 2359312-byte allocation
12-26 18:03:47.866: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 12% free 15157K/17223K, paused 12ms+2ms, total 48ms
12-26 18:03:47.866: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 4ms
12-26 18:03:47.886: D/dalvikvm(24982): GC_FOR_ALLOC freed 1024K, 18% free 14139K/17223K, paused 19ms, total 19ms
12-26 18:03:47.926: D/dalvikvm(24982): GC_FOR_ALLOC freed <1K, 13% free 15115K/17223K, paused 17ms, total 17ms
12-26 18:03:47.946: I/dalvikvm-heap(24982): Grow heap (frag case) to 17.481MB for 2250016-byte allocation
12-26 18:03:47.976: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 12% free 17312K/19463K, paused 13ms+2ms, total 29ms
12-26 18:03:48.076: D/dalvikvm(24982): GC_FOR_ALLOC freed 1232K, 14% free 16922K/19463K, paused 27ms, total 27ms
12-26 18:03:48.076: I/dalvikvm-heap(24982): Grow heap (frag case) to 18.099MB for 1048592-byte allocation
12-26 18:03:48.186: D/dalvikvm(24982): GC_FOR_ALLOC freed <1K, 13% free 17946K/20551K, paused 41ms, total 41ms
12-26 18:03:48.226: D/dalvikvm(24982): GC_FOR_ALLOC freed 0K, 13% free 17946K/20551K, paused 43ms, total 43ms
12-26 18:03:48.256: I/dalvikvm-heap(24982): Grow heap (frag case) to 20.349MB for 2359312-byte allocation
12-26 18:03:48.336: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 12% free 20250K/22919K, paused 30ms+13ms, total 88ms
12-26 18:03:48.336: D/dalvikvm(24982): WAIT_FOR_CONCURRENT_GC blocked 32ms
12-26 18:03:48.416: D/dalvikvm(24982): GC_FOR_ALLOC freed 1024K, 17% free 19231K/22919K, paused 59ms, total 65ms
12-26 18:03:48.426: I/dalvikvm-heap(24982): Grow heap (frag case) to 20.730MB for 1442416-byte allocation
12-26 18:03:48.536: D/dalvikvm(24982): GC_FOR_ALLOC freed <1K, 16% free 20640K/24391K, paused 40ms, total 40ms
12-26 18:03:48.626: D/dalvikvm(24982): GC_FOR_ALLOC freed 0K, 16% free 20640K/24391K, paused 66ms, total 66ms
12-26 18:03:48.656: I/dalvikvm-heap(24982): Grow heap (frag case) to 23.827MB for 3247216-byte allocation
12-26 18:03:48.746: D/dalvikvm(24982): GC_CONCURRENT freed 0K, 14% free 23811K/27591K, paused 13ms+3ms, total 93ms
12-26 18:03:48.786: D/AndroidRuntime(24982): Shutting down VM
12-26 18:03:48.786: W/dalvikvm(24982): threadid=1: thread exiting with uncaught exception (group=0x4116b2a0)
12-26 18:03:48.806: E/AndroidRuntime(24982): FATAL EXCEPTION: main
12-26 18:03:48.806: E/AndroidRuntime(24982): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifisurveillance/com.example.wifisurveillance.myMainScreen}: java.lang.NullPointerException
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.os.Looper.loop(Looper.java:137)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread.main(ActivityThread.java:4856)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at java.lang.reflect.Method.invoke(Method.java:511)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at dalvik.system.NativeStart.main(Native Method)
12-26 18:03:48.806: E/AndroidRuntime(24982): Caused by: java.lang.NullPointerException
12-26 18:03:48.806: E/AndroidRuntime(24982):    at com.example.wifisurveillance.myMainScreen.onCreate(myMainScreen.java:45)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.Activity.performCreate(Activity.java:5047)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-26 18:03:48.806: E/AndroidRuntime(24982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
12-26 18:03:48.806: E/AndroidRuntime(24982):    ... 11 more
12-26 18:04:06.005: I/Process(24982): Sending signal. PID: 24982 SIG: 9
12-26 18:03:42.040:E/Trace(24982):打开跟踪文件时出错:没有这样的文件或目录(2)
12-26 18:03:42.280:D/dalvikvm(24982):全部释放70K,8%释放9384K/10115K,暂停31ms,总计31ms
12-26 18:03:42.290:I/dalvikvm堆(24982):为466576字节分配将堆(frag大小写)增加到10.183MB
12-26 18:03:42.330:D/dalvikvm(24982):GC_并发释放1K,8%释放9839K/10631K,暂停14ms+2ms,总计39ms
12-26 18:03:42.330:D/dalvikvm(24982):等待并发GC阻塞15毫秒
12-26 18:03:42.350:D/dalvikvm(24982):释放0千分之八释放9839K/10631K,暂停23毫秒,总计23毫秒
12-26 18:03:42.350:I/dalvikvm堆(24982):对于1049776字节分配,将堆(frag大小写)增加到11.183MB
12-26 18:03:42.400:D/dalvikvm(24982):GC_并发释放0K,8%释放10864K/11719K,暂停18ms+3ms,总计47ms
12-26 18:03:42.400:D/dalvikvm(24982):等待并发GC阻塞19ms
12-26 18:03:42.420:D/InputTransport(24982):构建的输入通道:name='421455b8 com.example.wifisurveillance/com.example.wifisurveillance.MainActivity(客户端)',fd=41
12-26 18:03:42.550:D/libEGL(24982):loaded/system/lib/egl/libEGL_.so
12-26 18:03:42.560:D/libEGL(24982):loaded/system/lib/egl/libGLESv1_CM_mali.so
12-26 18:03:42.570:D/libEGL(24982):loaded/system/lib/egl/libGLESv2_.so
12-26 18:03:42.600:D/OpenGLRenderer(24982):启用调试模式0
12-26 18:03:47.545:D/dalvikvm(24982):全部释放471K,11%释放10474K/11719K,暂停17ms,总计17ms
12-26 18:03:47.545:I/dalvikvm堆(24982):为614416字节分配将堆(frag案例)增长到11.388MB
12-26 18:03:47.575:D/dalvikvm(24982):GC_并发释放2K,11%释放11072K/12359K,暂停13ms+1ms,总计36ms
12-26 18:03:47.575:D/dalvikvm(24982):等待并发GC阻塞13毫秒
12-26 18:03:47.605:D/dalvikvm(24982):释放0公里时的总释放量,11%释放11072K/12359K,暂停20毫秒,总计20毫秒
12-26 18:03:47.605:I/dalvikvm堆(24982):为1382416字节分配将堆(frag案例)增长到12.705MB
12-26 18:03:47.645:D/dalvikvm(24982):GC_并发释放0公里,10%释放12422K/13767K,暂停15毫秒+2毫秒,总计37毫秒
12-26 18:03:47.645:D/dalvikvm(24982):等待并发GC阻塞5毫秒
12-26 18:03:47.675:D/dalvikvm(24982):释放600K的全部,15%的释放11829K/13767K,暂停17ms,总计17ms
12-26 18:03:47.685:I/dalvikvm堆(24982):为1048592字节分配将堆(frag大小写)增长到13.125MB

12-26 18:03:47.735:D/dalvikvm(24982):GC_并发释放布局中没有
按钮4
,因此
findViewById()
返回空值。在
null
上调用
setOnClickListener()
会导致异常


button4
标识符本身是由布局
@+id/button4
引用引入的。

你能发布崩溃的痕迹吗?你能发布你的xml布局吗?你能给出logcat吗?何时何地崩溃?
<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"
android:background="@drawable/back"
android:gravity="start"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="10dp"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button2"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/imageView1"
    android:background="?android:selectableItemBackground"
    android:layout_marginTop="17dp"
    android:text="@string/B2" 
    android:textColor="@android:color/white"
    android:textSize="24sp"
    android:gravity="center"


    />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignTop="@+id/button2"
    android:src="@drawable/gallery" />

<Button
    android:id="@+id/button3"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignRight="@+id/button2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="17dp"
    android:text="@string/B3" 
    android:textColor="@android:color/white"
    android:textSize="23sp"
    android:background="?android:selectableItemBackground"
    android:gravity="center|right"/>

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button3"
    android:layout_alignTop="@+id/button3"
    android:src="@drawable/sensor" />

 <Button
    android:id="@+id/Button04"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button3"
    android:layout_below="@+id/button3"
    android:background="?android:selectableItemBackground"
    android:gravity="center"
    android:text="@string/B4"
    android:textColor="@android:color/white"
    android:textSize="24sp" 
    android:layout_marginTop="17dp"/>

 <ImageView
    android:id="@+id/imageView4"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/Button04"
    android:layout_alignTop="@+id/Button04"
    android:src="@drawable/log" />

 <Button
    android:id="@+id/button5"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/Button04"
    android:layout_alignRight="@+id/Button04"
    android:layout_below="@+id/Button04"
    android:text="@string/B5" 
    android:textColor="@android:color/white"
    android:textSize="24sp" 
    android:layout_marginTop="17dp"
    android:background="?android:selectableItemBackground"/>

 <ImageView
    android:id="@+id/imageView5"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button5"
    android:layout_alignTop="@+id/button5"
    android:src="@drawable/help" />

 <Button
    android:id="@+id/button6"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button4"
    android:layout_alignRight="@+id/button4"
    android:layout_below="@+id/button5"
    android:layout_marginTop="17dp"
    android:text="@string/B6" 
    android:textColor="@android:color/white"
    android:textSize="24sp" 
    android:background="?android:selectableItemBackground"/>

 <ImageView
    android:id="@+id/imageView6"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button6"
    android:layout_alignTop="@+id/button6"
    android:src="@drawable/exit" />

 <Button
    android:id="@+id/button1"
    android:layout_width="425dp"
    android:layout_height="60dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="?android:selectableItemBackground"
    android:gravity="center|right"
    android:text="@string/B1"
    android:textColor="@android:color/white"
    android:textSize="24sp" />

</RelativeLayout>