Java 当按下菜单上的按钮时,Android、Google电子表格崩溃

Java 当按下菜单上的按钮时,Android、Google电子表格崩溃,java,android,logcat,Java,Android,Logcat,嘿,伙计们,我的当前应用程序有一个问题,问题是出于某种原因,每当我尝试单击菜单中的“保存注册表”按钮时,应用程序就会崩溃。它说的是println消息,但我肯定我已经把helloworld放在一个字符串中了 我想提及的是,我一直在遵循Youtube上的一个教程,将数据发布到google电子表格和其他教程来创建此AP管理器,您可能已经注意到我的代码中,但下一步是将数组列表保存到电子表格中,这是下一步。但是现在我想我不能简单地用菜单按钮将“Hello World”信息保存到电子表格中 我的日志显示:

嘿,伙计们,我的当前应用程序有一个问题,问题是出于某种原因,每当我尝试单击菜单中的“保存注册表”按钮时,应用程序就会崩溃。它说的是println消息,但我肯定我已经把helloworld放在一个字符串中了

我想提及的是,我一直在遵循Youtube上的一个教程,将数据发布到google电子表格和其他教程来创建此AP管理器,您可能已经注意到我的代码中,但下一步是将数组列表保存到电子表格中,这是下一步。但是现在我想我不能简单地用菜单按钮将“Hello World”信息保存到电子表格中

我的日志显示:

Process: com.example.gavin.wifiattendance, PID: 2266
java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:160)
        at com.example.gavin.wifiattendance.MainActivity.postData(MainActivity.java:93)
        at com.example.gavin.wifiattendance.MainActivity.onMenuItemSelected(MainActivity.java:143)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1127)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
        at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:165)
        at android.widget.AdapterView.performItemClick(AdapterView.java:300)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
我的主要活动文件:

public class MainActivity extends Activity{

boolean wasApEnabled = false;
static AccessPoint wifiAP;
private WifiManager wifi;
static Button apButton;
static TextView textView;
final String myTag = "DocsUpload";


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

    apButton = (Button) findViewById(R.id.toggleBtn);
    textView = (TextView) findViewById(R.id.wifiClients);

    apButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            wifiAP.toggleWifiAP(wifi, MainActivity.this);
        }
    });

    wifiAP = new AccessPoint(this);
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    scan();

    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_DIM_BEHIND);

}

private void scan(){
    wifiAP.getClientList(false, new FinishScanListener() {

        @Override
        public void onFinishScan(final ArrayList<ClientScanResult> clients) {
            textView.setText("WifiApState:" + wifiAP.getWifiApState()+ "\n\n");
            textView.append("Clients: \n");
            for (ClientScanResult clientScanResult : clients){
                textView.append("====================\n");
                textView.append("ipAddress: " + clientScanResult.getIpAddress() + "\n");
                textView.append("Device: " + clientScanResult.getDevice() + "\n");
                textView.append("macAddress: " + clientScanResult.getMacAddress() + "\n");
                textView.append("isReachable: " + clientScanResult.isReachable() + "\n");

            }
        }
    });
}

public void postData() {

    String fullUrl = "https://docs.google.com/forms/d/1yipuuXd5V53Ol12U24Cl2H4RIdYtm622jIk13Zo26cM/formResponse";
    HttpRequest mReq = new HttpRequest();
    String col1 = "Hello";
    String col2 = "World";

    String data = "entry_272641491=" + URLEncoder.encode(col1) + "&" +
            "entry_130393492=" + URLEncoder.encode(col2);
    String response = mReq.sendPost(fullUrl, data);
    Log.i(myTag, response);
}

@Override
public void onResume() {
    super.onResume();
    if (wasApEnabled) {
        if (wifiAP.getWifiApState() != wifiAP.WIFI_STATE_ENABLED && wifiAP.getWifiApState() != wifiAP.WIFI_STATE_ENABLING) {
            wifiAP.toggleWifiAP(wifi, MainActivity.this);
        }
    }
    updateStatusDisplay();
}

@Override
public void onPause() {
    super.onPause();
    boolean wifiApIsOn = wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLED || wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLING;
    if (wifiApIsOn){
        wasApEnabled = true;
        wifiAP.toggleWifiAP(wifi, MainActivity.this);
    }else {
        wasApEnabled = false;
    }
    updateStatusDisplay();
}

public static void updateStatusDisplay(){
    if (wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLED || wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLING){
        apButton.setText("Turn Off");
    }else {
        apButton.setText("Turn on");
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,0,0, "Get Clients");
    menu.add(0,1,0, "Save Register");
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()){
        case 0:
            scan();
            break;
        case 1:
            postData();
            break;
    }
    return super.onMenuItemSelected(featureId, item);
}

您正在向postData中的Log.i传递空字符串。不要那样做。这几乎可以肯定,因为HTTP请求是异步的,您还没有响应。因为它只是一个日志,所以我会删除该行。

Hello tht解决了我的问题,但在我删除它之后,它将不会向电子表格发送任何数据。我在logcat中收到这条消息,我将在我的原始消息中为其创建一个编辑
    03-12 11:41:56.444    1903-1921/com.example.gavin.wifiattendance W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5c08180, error=EGL_SUCCESS
03-12 11:41:58.696    1903-1903/com.example.gavin.wifiattendance D/WifiAttendance﹕ Setting httpPost headers
03-12 11:41:58.696    1903-1903/com.example.gavin.wifiattendance D/Your App Name Here﹕ https://docs.google.com/forms/d/1yipuuXd5V53Ol12U24Cl2H4RIdYtm622jIk13Zo26cM/formResponse?entry_272641491=Hello&entry_130393492=World
03-12 11:41:58.697    1903-1903/com.example.gavin.wifiattendance E/WifiAttendance﹕ HttpUtils: android.os.NetworkOnMainThreadException
03-12 11:41:58.697    1903-1903/com.example.gavin.wifiattendance D/WifiAttendance﹕ Returning value:null