gpt4 book ai didi

java - 实现 onClickListener 不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:03:40 25 4
gpt4 key购买 nike

我正在尝试连接一个按钮,按下该按钮时会显示纬度和经度。它似乎没有用,似乎无法弄清楚原因。任何帮助将不胜感激。

ShowActivityLocation.java:

package com.democo.android.locationapi.simple;

import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latitudeField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_location); // Your main activity
latitudeField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);

StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latitudeField.setText("Location not available");
longitudeField.setText("Location not available");
}
}

@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);
Button btngetLoc = (Button) findViewById(R.id.getLoc);

btngetLoc.setOnClickListener(new OnClickListener() {

public void onClick(View view) {
onLocationChanged();
}

});
return true;
}

/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latitudeField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));

new Thread(new Runnable() {
@Override
public void run() {
sendPosToServer(location.getLatitude(), location.getLongitude());
}
}).run();

}

public void sendPosToServer(double lat, double lon) {
URL url;
HttpURLConnection conn;
BufferedReader rd;

String fullURL="http://10.0.2.2:8080/LocationRecorder/UploadLocation'lat='+lat+'lon='+lon";

try {
Log.i(“SendPos”,"Sending request for sensor data to: "+fullURL);
url = new URL(fullURL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// Issue the GET to send the data
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// read the result to process response and ensure data sent
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}

布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:orientation="horizontal" >

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="Latitude: "
android:textSize="20dip" >
</TextView>

<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unknown"
android:textSize="20dip" >
</TextView>
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="Longitude: "
android:textSize="20dip" >
</TextView>

<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unknown"
android:textSize="20dip" >
</TextView>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:id="@+id/getLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"
android:onClick="clickFunc"
/>

</LinearLayout>

最佳答案

对于初学者来说,您的按钮不在您的布局页面线性布局中,

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/linearLayout1" >
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2">

</LinearLayout>

<LinearLayout >

<Button
android:id="@+id/getLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"
//android:onClick="clickFunc"
/>
</LinearLayout>
// Add this
</LinearLayout>

您还需要将其添加到您的 onCreate 方法中,将其从您的 onCreateOptionsMenu() 中取出

然后在 oncreate() 中:

Button btngetLoc = (Button) findViewById(R.id.getLoc);

.../...
});

并且如前所述删除了 android:onClick="clickFunc" 因为你没有这个方法并且你已经为按钮创建了一个 onclicklistener。


你打电话:

onLocationChanged();

它接受参数 Location 位置

 public void onLocationChanged(Location location) {

所以下面的代码都不起作用:

int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latitudeField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));

我倾向于将 Location location 设为初始化为 null 的类变量(默认情况下会这样),然后在您的方法中使用位置检查

if(location!=null)

除此之外,我认为如果您将此工作进行到这个阶段,我建议您提出一个新问题,因为它有点长。尝试分别处理以下两个问题:

  1. 您如何获得您的位置(测试它/记录它以查看您是否获得位置)

  2. 在线通信(运行单独的测试以查看您的通信代码是否正常工作)

您会发现这更容易调试。

因此,如果您的应用在编辑后仍然崩溃,我建议您发布 create a Minimal, Complete, and Verifiable example .正如我在您的屏幕截图中注意到的那样,您正在到处更改代码,在这种情况下无法回答。

请包含您的 logcat。

关于java - 实现 onClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com