您好我是Android应用程序开发和Java的新手。我使用了一些来自互联网的代码和Android工作室生成的代码。它需要通过插座发送一个字符串到一个wifi盾牌来控制一艘带货物的小船。当我调试我的应用程序时它会启动,但是当我点击任何按钮时它会崩溃。我试图使用logcat来查找问题,但它没有向我显示任何内容。我使用了一些错误范围,我在stackoverflow上找到它,它在设备上设置。当我尝试使用seekBar时,应用程序不会崩溃,点击后也不会显示吐司。
在对代码运行检查后,我发现了这个:
'public'方法buttonConnectOnClickListener()未通过第69行的接口公开
它向我展示了每个按钮监听器上的相同问题,我不知道如何修复它。
这是Main.java:
package tomasbenco.kop_android_controller;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main extends Activity {
private Button C1Button = null;
private Button C2Button = null;
private Switch lightsSwitch = null;
private SeekBar RESB = null;
private SeekBar LESB = null;
private Boolean connected = Boolean.FALSE;
private DataOutputStream dataOutputStream = null;
private Socket socket = null;
private char[] transferReg = new char[7];
private void setTransferReg(char transferReg, int i) {
this.transferReg[i] = transferReg;
}
private char[] getTransferReg() {
return transferReg;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
C1Button = (Button) findViewById(R.id.C1Button);
C2Button = (Button) findViewById(R.id.C2Button);
lightsSwitch = (Switch) findViewById(R.id.LightsSwitch);
RESB = (SeekBar) findViewById(R.id.REseekBar);
LESB = (SeekBar) findViewById(R.id.LEseekBar);
}
private void changeConnectionStatus(Boolean isConnected) {
connected = isConnected;
if (isConnected) {
C1Button.setVisibility(View.VISIBLE);
C2Button.setVisibility(View.VISIBLE);
lightsSwitch.setVisibility(View.VISIBLE);
RESB.setVisibility(View.VISIBLE);
LESB.setVisibility(View.VISIBLE);
} else {
C1Button.setVisibility(View.INVISIBLE);
C2Button.setVisibility(View.INVISIBLE);
lightsSwitch.setVisibility(View.INVISIBLE);
RESB.setVisibility(View.INVISIBLE);
LESB.setVisibility(View.INVISIBLE);
}
}
public void buttonConnectOnClickListener(View view) {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isConnected()) {
if (!connected) {
try {
socket = new Socket("192.168.137.1", 55555);
Toast msg = Toast.makeText(Main.this,
"Connected Successfully", Toast.LENGTH_LONG);
msg.show();
dataOutputStream = new DataOutputStream(socket.getOutputStream());
changeConnectionStatus(Boolean.TRUE);
} catch (IOException e) {
changeConnectionStatus(Boolean.FALSE);
}
} else {
try {
socket.close();
Toast msg = Toast.makeText(Main.this, "Disconnected Successfully", Toast.LENGTH_LONG);
msg.show();
changeConnectionStatus(Boolean.FALSE);
} catch (UnknownHostException e) {
changeConnectionStatus(Boolean.FALSE);
} catch (IOException e) {
changeConnectionStatus(Boolean.FALSE);
}
}
} else {
Toast msg = Toast.makeText(Main.this, "Wifi is not connected !", Toast.LENGTH_LONG);
msg.show();
}
}
public void C1ButtonOnClickListener(View view) {
if (C1Button.isActivated()) {
Toast msg = Toast.makeText(Main.this, "Front cargo bay is opening !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(4, '1');
} else {
Toast msg = Toast.makeText(Main.this, "Front cargo bay is closing !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(4, '0');
}
}
public void C2ButtonOnClickListener(View view) {
if (C2Button.isActivated()) {
Toast msg = Toast.makeText(Main.this, "Back cargo bay is opening !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(5, '1');
} else {
Toast msg = Toast.makeText(Main.this, "Back cargo bay is closing !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(5, '0');
}
}
public void lightsButtonOnClickListener(View view) {
if (C2Button.isActivated()) {
Toast msg = Toast.makeText(Main.this, "Back cargo bay is opening !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(6, '1');
} else {
Toast msg = Toast.makeText(Main.this, "Back cargo bay is closing !", Toast.LENGTH_LONG);
msg.show();
transfer_REG(6, '0');
}
}
private void transfer_REG(int i, char trans) {
setTransferReg(trans, i);
String transfer = new String(getTransferReg());
if (connected) try {
dataOutputStream.writeChars(transfer);
} catch (IOException ignored) {
}
}
public void REListener(View view) {
if (RESB.getProgress() < 10) {
transfer_REG(2, Integer.toString(RESB.getProgress()).charAt(0));
transfer_REG(3, '1');
} else {
transfer_REG(2, Integer.toString(RESB.getProgress() - 10).charAt(0));
transfer_REG(3, '0');
}
}
public void LEListener(View view) {
if (LESB.getProgress() < 10) {
transfer_REG(1, Integer.toString(LESB.getProgress()).charAt(0));
transfer_REG(2, '1');
Toast msg = Toast.makeText(Main.this, "Left engine power is: -" + Integer.toString(LESB.getProgress()).charAt(0), Toast.LENGTH_LONG);
msg.show();
} else {
transfer_REG(1, Integer.toString(LESB.getProgress() - 10).charAt(0));
transfer_REG(2, '0');
Toast msg = Toast.makeText(Main.this, "Left engine power is: " + Integer.toString(LESB.getProgress() - 10).charAt(0), Toast.LENGTH_LONG);
msg.show();
}
}
}
这是Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@android:style/Theme.Holo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main">
<TextView
android:id="@+id/REtView"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/REseekBar"
android:layout_alignParentEnd="true"
android:text="@string/RE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffaec0ff"
android:textStyle="bold|italic"
android:theme="@style/Holo"
android:visibility="visible" />
<TextView
android:id="@+id/LEtView"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/LEseekBar"
android:layout_alignParentStart="true"
android:text="@string/LE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffaec0ff"
android:textStyle="bold|italic"
android:theme="@style/Holo"
android:visibility="visible" />
<SeekBar
android:id="@+id/LEseekBar"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/LightsSwitch"
android:clickable="true"
android:max="20"
android:onClick="LEListener"
android:progress="10"
android:theme="@style/Holo"
android:visibility="visible" />
<SeekBar
android:id="@+id/REseekBar"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/REtView"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/LightsSwitch"
android:clickable="true"
android:max="20"
android:onClick="REListener"
android:progress="10"
android:theme="@style/Holo"
android:visibility="visible" />
<Button
android:id="@+id/connButton"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="buttonConnectOnClickListener"
android:text="@string/conn"
android:textColor="#ffaec0ff"
android:textStyle="normal"
android:theme="@style/Holo"
android:typeface="normal"
android:visibility="visible" />
<Button
android:id="@+id/C1Button"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:onClick="C1ButtonOnClickListener"
android:text="@string/C1"
android:textColor="#ffaec0ff"
android:textStyle="normal"
android:theme="@style/Holo"
android:visibility="visible" />
<Button
android:id="@+id/C2Button"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/C1Button"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="C2ButtonOnClickListener"
android:text="@string/C2"
android:textColor="#ffaec0ff"
android:textStyle="normal"
android:theme="@style/Holo"
android:visibility="visible" />
<Switch
android:id="@+id/LightsSwitch"
style="@android:style/Theme.Holo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/C2Button"
android:layout_centerHorizontal="true"
android:checked="true"
android:clickable="true"
android:onClick="lightsButtonOnClickListener"
android:showText="false"
android:singleLine="true"
android:text="@string/light"
android:textColor="#ffaec0ff"
android:textStyle="normal"
android:theme="@style/Holo"
android:visibility="visible" />
这是manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo">
<activity
android:name=".Main"
android:configChanges="keyboard|keyboardHidden"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
尝试以下代码,
按照两个按钮的其余部分全局声明按钮;
private Button connButton;
将此代码放在您声明所有ID的位置。
connButton = (Button) findViewById(R.id.connButton);
答案 1 :(得分:0)
你能否在Android清单中看到你是否拥有此权限?
.libPaths()
我试图复制你的代码并执行,这是我用顶部按钮得到的问题。在Android清单中添加此功能后,问题就会得到解决。
由于您没有发布AndroidManifest.xml,因此很难说您可能在那里声明了哪个权限。那么你可以检查一下是否已经存在,如果没有,你可以添加它并尝试吗?
我希望这会有所帮助。