Java Android 清单 - 带有 Activity/Runnable 类的“没有默认构造函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25924465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Manifest - "Has No Default Constructor" With Activity/Runnable Class
提问by Finn C
I have a fairly confusing problem. I'm trying to run a basic chat client via Android. I've set it up within 3 classes of my main project. Problem is, for some odd reason, my ChatConnect.java (which handles actual chat messaging) isn't seeming to pop up as an Activity for AndroidManifest.xml, which is causing some serious issues - AKA I need to use a Layout (specifically game.xml) in my ChatConnect class and it refuses to load as a result of not being defined as an activity in the manifest. Anyways, here's my three classes.
我有一个相当令人困惑的问题。我正在尝试通过 Android 运行一个基本的聊天客户端。我已经在我的主要项目的 3 个班级中设置了它。问题是,出于某种奇怪的原因,我的 ChatConnect.java(处理实际聊天消息)似乎没有作为 AndroidManifest.xml 的 Activity 弹出,这导致了一些严重的问题 - 也就是我需要使用布局(特别是game.xml) 在我的 ChatConnect 类中,并且由于未在清单中定义为活动而拒绝加载。无论如何,这是我的三个班级。
Yes, I realize StrictMode is horribly awful. However, I also can't get the chat client to work without it, even with said permissions in manifest. I've tried cleaning my project.
是的,我意识到 StrictMode 非常糟糕。但是,即使在清单中具有上述权限,我也无法让聊天客户端在没有它的情况下工作。我试过清理我的项目。
All help is greatly appreciated!
非常感谢所有帮助!
ChatConnect.java
聊天连接程序
package com.example.AndroidRPGNew.multiplayer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.AndroidRPGNew.Main;
import com.example.AndroidRPGNew.R;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ChatConnect extends Activity implements Runnable {
// Begin displaying messages to game.xml. Display to chatView via new lines.
// Ability to send message via chatMessageSend - Sends chat message data from chatMessage text field
// Once connected, log to chat. Allow for multicolors, etc.
private Socket socket;
public String userId;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
SharedPreferences settings = getSharedPreferences(Main.PREFS_NAME, 0);
userId = settings.getString("userId", "unknown");
run();
}
public ChatConnect(Socket s){
socket = s;
}
public void run(){
try{
final Scanner chat = new Scanner(System.in);
final Scanner in = new Scanner(socket.getInputStream());
final PrintWriter out = new PrintWriter(socket.getOutputStream());
Button sendMessage = (Button) findViewById(R.id.chatMessageSend); // ERROR HERE: ALTHOUGH IT IS SUPPOSED TO BE IN GAME.XML CONTENT VIEW, THIS CAUSES A NULLPOINTER!
sendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView input = (TextView) findViewById(R.id.chatMessage);
String inputMsg = input.toString();
out.println(inputMsg);
out.flush();
if(in.hasNext()){
System.out.println(in.nextLine());
}
}
});
while(true){
String input = chat.nextLine();
out.println(input);
out.flush();
if(in.hasNext()){
System.out.println(in.nextLine());
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.AndroidRPGNew"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="com.example.AndroidRPGNew.Main"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.example.AndroidRPGNew.SettingsHandler"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.StoreHandler"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.Loading"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.MusicInitiator"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.AccountCreate"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.AccountSetup"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.MultiplayerMenu"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.SQLConnection"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.ServerConnect"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity android:name="com.example.AndroidRPGNew.multiplayer.ChatConnect"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NETWORK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
</manifest>
ServerConnect.java
服务器连接程序
package com.example.AndroidRPGNew.multiplayer;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import com.example.AndroidRPGNew.R;
import java.net.Socket;
/**
* Created by fccardiff on 9/18/14.
*/
public class ServerConnect extends Activity {
// Establish connection to server, with IP from MultiplayerMenu
// Initiate ChatConnect
String userId = null;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.game);
// TODO: KEEP THE ABOVE TWO LINES ONLY TEMPORARILY - FIND A FIX!
connect();
}
public void connect() {
final int port = 2525;
final String IP = MultiplayerMenu.getIP();
try {
Socket s = new Socket(IP, port);
Log.w("Server:", "Connected to " + IP + ":" + port);
ChatConnect client = new ChatConnect(s);
Thread thread = new Thread(client);
thread.start();
} catch (Exception serverNotFound) {
serverNotFound.printStackTrace();
}
}
}
回答by drewhannay
Android Activity
classes must have a default constructor that takes no parameters. Your ChatConnect
class has this constructor:
AndroidActivity
类必须有一个不带参数的默认构造函数。你的ChatConnect
类有这个构造函数:
public ChatConnect(Socket s){
socket = s;
}
But the system is looking for one like this:
但是系统正在寻找这样的:
public ChatConnect(){
}
and not finding one, which is why it's crashing.
并没有找到,这就是它崩溃的原因。
回答by Imran Khan
public ChatConnect(Socket s){
socket = s;
}
Remove this constructor. Your activity has constructor. do not define constructor for activity.
删除此构造函数。您的活动具有构造函数。不要为活动定义构造函数。