OnClickListener中的错误;无法启动Activity

时间:2016-02-19 07:46:12

标签: java android xml android-studio

我创建了一个按钮,当我点按它时,它应该启动Activity Wifi。 当我测试这段代码时,除了这个错误之外没有任何事情发生:

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

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

清单:

<?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活动布局):

<?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(我正在尝试的活动):

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您在Wifi活动中使用了布局R.id.BouttonWifi,但该布局文件中没有ID为NullPointerException的按钮。

我确信您在运行此代码时收到R.layout.content_main_wifi

解决方案:向标识为R.id.BouttonWifi的布局 console.log(xmlHttp.responseTex); 添加按钮。