简体   繁体   English

OnClickListener中的错误; 无法启动活动

[英]Error in an OnClickListener; can't start Activity

I created a button and I when I tap on it, it should start the Activity Wifi . 我创建了一个按钮,当我点击它时,它应该启动Activity Wifi When I test this code, nothing happens apart from this error: 当我测试此代码时,除此错误外,什么都没有发生:

02-19 08:34:33.455 10528-10528/test.sterela.com.sterelaapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

Wifi.java (the Activity where I have the button): Wifi.java(活动所在的按钮):

package test.sterela.com.sterelaapplication;

/**
 * Created by DB020490 on 17/02/2016.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Wifi extends Activity {
Intent myIntent=null;
@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.content_main_wifi);

    final Button button = (Button) findViewById(R.id.BouttonWifi);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Perform action on click
            myIntent = new Intent(Wifi.this, WifiActivity.class);
            startActivity(myIntent);
        }
    });
}
}

Manifest : 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.sterela.com.sterelaapplication">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Accueil_Parametres"/>

    <activity
        android:name=".WifiActivity" />
</application>

</manifest>

content_main_wifi.xml (Wifi Activity layout): content_main_wifi.xml(Wifi活动布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_height="fill_parent"
    android:layout_width="10dp"
    android:background="@android:color/white"
    android:id="@+id/ForceSignal"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp">

    <TextView
        android:id="@+id/tvWifiName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AP Name" />

    <TextView
        android:id="@+id/tvWifiMac"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:text="00:00:00:00:00:00" />

</LinearLayout>

</LinearLayout>

WifiActivity.java (the Activity I'm trying to start): WifiActivity.java(我正在尝试启动的活动):

package test.sterela.com.sterelaapplication;

import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.content.Context;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;

public class WifiActivity extends Activity {

    private Button boutonRechercher;
    private ListView listeViewWifi;
    private List<WifiItem> listeWifiItem;
    private WifiAdapter wifiAdapter;
    private WifiManager wifiManager;
    private WifiBroadcastReceiver broadcastReceiver;

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

    listeViewWifi = (ListView) findViewById(R.id.listViewWifi);
    boutonRechercher = (Button) findViewById(R.id.buttonRefresh);

    boutonRechercher.setOnClickListener (new View.OnClickListener()
    {
        public void onClick(View v)
        {
            if(wifiManager != null)
                wifiManager.startScan();
        }
    });

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    listeWifiItem = new ArrayList<WifiItem>();
    wifiAdapter = new WifiAdapter(this, listeWifiItem);
    listeViewWifi.setAdapter(wifiAdapter);

    broadcastReceiver = new WifiBroadcastReceiver();

    registerReceiver(broadcastReceiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

}

@Override
protected void onPause() {
    unregisterReceiver(broadcastReceiver);
    super.onPause();
}

@Override
protected void onResume() {
    registerReceiver(broadcastReceiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}


public WifiManager getCurrentWifiManager() {
    return wifiManager;
}

public WifiAdapter getWifiAdapter() {
    return wifiAdapter;
}

public List<WifiItem> getListeWifiItem() {
    return listeWifiItem;
}
}

Thanks for your help! 谢谢你的帮助!

You are using layout R.layout.content_main_wifi inside Wifi activity, but there is no button in that layout file with id R.id.BouttonWifi . 您正在Wifi活动中使用布局R.layout.content_main_wifi ,但该布局文件中没有ID为R.id.BouttonWifi

I am sure you are getting a NullPointerException while running this code. 我确定您在运行此代码时会收到NullPointerException

Solution: Add a button to layout R.layout.content_main_wifi with id R.id.BouttonWifi . 解决方案:将一个按钮添加到ID为R.id.BouttonWifi布局R.layout.content_main_wifi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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