2014-09-19 17 views
10

Tôi gặp vấn đề khá khó hiểu. Tôi đang cố gắng chạy ứng dụng trò chuyện cơ bản qua Android. Tôi đã thiết lập nó trong 3 lớp học của dự án chính của tôi. Vấn đề là, vì một số lý do kỳ lạ, ChatConnect.java (xử lý tin nhắn trò chuyện thực sự của tôi) dường như không bật lên như một Activity cho AndroidManifest.xml, điều này gây ra một số vấn đề nghiêm trọng - AKA Tôi cần sử dụng một Layout (cụ thể) game.xml) trong lớp ChatConnect của tôi và từ chối tải do không được định nghĩa là hoạt động trong tệp kê khai. Dù sao, đây là ba lớp của tôi.Tệp kê khai Android - "Không có trình tạo mặc định" với hoạt động/Lớp có thể chạy được

Có, tôi nhận thấy StrictMode khủng khiếp khủng khiếp. Tuy nhiên, tôi cũng không thể yêu cầu ứng dụng trò chuyện làm việc mà không có nó, ngay cả với quyền được cho phép trong tệp kê khai. Tôi đã thử làm sạch dự án của mình.

Tất cả trợ giúp đều được đánh giá cao!

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

<?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(); 
     } 
    } 
} 
+0

Tôi không chắc chắn chính xác vấn đề ở đây ... nó "dường như không bật lên như một Hoạt động cho AndroidManifest.xml"? – drewhannay

+1

Với tên của chúng, một số hoạt động của bạn thực sự là hoạt động. Các hoạt động dành cho giao diện người dùng. Chúng không phải là các lớp Java có mục đích chung mà bạn sử dụng cho các ổ cắm, hoặc các cơ sở dữ liệu, hoặc bất cứ cái gì mà một 'SettingsHandler' là. – CommonsWare

Trả lời

19

Android Activity lớp phải có một constructor mặc định rằng sẽ không có thông số. lớp ChatConnect của bạn có constructor này:

public ChatConnect(Socket s){ 
     socket = s; 
} 

Nhưng hệ thống đang tìm kiếm một như thế này:

public ChatConnect(){ 
} 

và không tìm thấy một, đó là lý do nó bị rơi.

Các vấn đề liên quan