2016-04-08 26 views
5

Na kliknięcie przycisku, aby przenieść się do innej działalności, awarii aplikacji i zalogować się pokazy:Pierwsze IllegalStateException przycisk kliknij

java.lang.IllegalStateException: Could not execute method for android:onClick 

do debugowania, starałem się przenieść do pustego aktywności nadal pokazuje ten sam błąd. Nie mogę zrozumieć, dlaczego!

To jest mój główny plik aktywność java i pokazuje błąd w przyjmującym() 's zamiar funkcję:

package com.example.unholyalliance.infinitestream; 

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

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

    public void host(View v) 
    { 
     Intent i = new Intent(this,Host.class); 
     try 
     { 
      startActivity(i); 
     }catch(IllegalStateException e) 
     { 
      Context context = getApplicationContext(); 
      CharSequence text = e.getMessage(); 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 
     } 
    } 

    public void search(View v) 
    { 

    } 
} 



This is my Host.java file: 

import android.content.Context; 

import android.net.nsd.NsdManager; 
import android.net.nsd.NsdServiceInfo; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 

import java.io.IOException; 
import java.net.ServerSocket; 

public class Host extends AppCompatActivity { 

    public String mServiceName="Stream"; 
    ServerSocket mServerSocket=null; 
    NsdManager.RegistrationListener mRegistrationListener=null; 
    private NsdManager mNsdManager=null; 
    int port=9000; 
    TextView service_status = (TextView) findViewById(R.id.textView1); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_host); 
     mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); 
     try { 
      initializeServerSocket(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void initializeServerSocket() throws IOException { 
     //Initialize a server socket on the next available port. 
     mServerSocket = new ServerSocket(0); 

     // Store the chosen port. 
     port = mServerSocket.getLocalPort(); 
     registerService(port); 

    } 

    public void registerService(int port) { 
     // Create the NsdServiceInfo object, and populate it. 
     NsdServiceInfo serviceInfo = new NsdServiceInfo(); 

     // The name is subject to change based on conflicts 
     // with other services advertised on the same network. 
     serviceInfo.setServiceName("Stream"); 
     serviceInfo.setServiceType("_http._tcp."); 
     serviceInfo.setPort(port); 
     mNsdManager.registerService(
       serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener); 
     initializeRegistrationListener(); 


    } 



    public void initializeRegistrationListener() { 
     mRegistrationListener = new NsdManager.RegistrationListener() { 

      @Override 
      public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) { 
       // Save the service name. Android may have changed it in order to 
       // resolve a conflict, so update the name you initially requested 
       // with the name Android actually used. 
       mServiceName = NsdServiceInfo.getServiceName(); 
       service_status.setText("Success"); 
      } 

      @Override 
      public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { 
       // Registration failed! Put debugging code here to determine why. 
       service_status.setText("Registration Failed!"); 
      } 

      @Override 
      public void onServiceUnregistered(NsdServiceInfo arg0) { 
       // Service has been unregistered. This only happens when you call 
       // NsdManager.unregisterService() and pass in this listener. 
       service_status.setText("Registration not done"); 
      } 

      @Override 
      public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { 
       // Unregistration failed. Put debugging code here to determine why. 
       service_status.setText("Unregistration failed"); 
      } 
     }; 
    } 
} 

Pełna Śledzenie stosu: Plik

E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.IllegalStateException: Could not execute method for android:onClick 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
    at android.view.View.performClick(View.java:4278) 
    at android.view.View$PerformClick.run(View.java:17429) 
    at android.os.Handler.handleCallback(Handler.java:725) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5099) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:4278)  
    at android.view.View$PerformClick.run(View.java:17429)  
    at android.os.Handler.handleCallback(Handler.java:725)  
    at android.os.Handler.dispatchMessage(Handler.java:92)  
    at android.os.Looper.loop(Looper.java:137)  
    at android.app.ActivityThread.main(ActivityThread.java:5099)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)  
    at dalvik.system.NativeStart.main(Native Method)  
    Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.unholyalliance.infinitestream/com.example.unholyalliance.infinitestream.Host}; have you declared this activity in your AndroidManifest.xml? 
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1633) 
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1425) 
    at android.app.Activity.startActivityForResult(Activity.java:3370) 
    at android.app.Activity.startActivityForResult(Activity.java:3331) 
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843) 
    at android.app.Activity.startActivity(Activity.java:3566) 
    at android.app.Activity.startActivity(Activity.java:3534) 
    at com.example.unholyalliance.infinitestream.MainActivity.host(MainActivity.java:23) 
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  
    at android.view.View.performClick(View.java:4278)  
    at android.view.View$PerformClick.run(View.java:17429)  
    at android.os.Handler.handleCallback(Handler.java:725)  
    at android.os.Handler.dispatchMessage(Handler.java:92)  
    at android.os.Looper.loop(Looper.java:137)  
    at android.app.ActivityThread.main(ActivityThread.java:5099)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)  
    at dalvik.system.NativeStart.main(Native Method)  

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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: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="com.example.unholyalliance.infinitestream.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Host" 
     android:id="@+id/host_button" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="104dp" 
     android:clickable="true" 
     android:onClick="host" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Search" 
     android:id="@+id/search_button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:onClick="search" 
     android:clickable="true" /> 
</RelativeLayout> 
+0

opublikować "stack_main.xml" –

+0

po stosie stosu. – njzk2

+0

Rzeczywiście, to, co George napisał, jest prawdą, ale nie powinno to trwać IllegalStateException na MainActivity. Więc nie sądzę, że to jest jego problem ... –

Odpowiedz

8

Kluczową częścią pełnego śledzenia stosu znajduje się tutaj:

Spowodowany przez: android.content.ActivityNotFoundException: Nie można znaleźć wyraźny klasę aktywności { com.example.unholyalliance.infinitestream/com.example.unholyalliance.infinitestream.Host}; masz ogłosił tę działalność w swoim AndroidManifest.xml

Wydaje się, że nie ma tego Host aktywność zadeklarowaną w pliku manifestu. Otwórz AndroidManifest.xml i sprawdzić lub dodać następujące:

<activity 
    android:name=".Host" /> 

Również upewnić się rozwiązać ten problem w Host najpierw tylko zadeklarować TextView a następnie przypisać wartość onCreate() po setContentView().

private TextView service_status; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_host); 
    service_status = (TextView) findViewById(R.id.textView1); 
    ... 
} 
+0

Wielkie dzięki ... Pracowałem ... to był naprawdę głupi błąd! – yash1996

+0

Nie ma za co. Cieszę się, że mogłem pomóc. –

0

Upewnij się, że Twój onClick w widoku Przycisków w activity_main.xml jest taki android:onClick="host"

Przykład:

<Button 
    android:id="@+id/main_activity_bt_host" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/host" 
    android:onClick="host" /> 
+0

Powiedziałbym, że to robi, w przeciwnym razie IllegalStateException nie zostałoby rzucone i nie wskazywałoby funkcji host(). –

+0

Aplikacja buforuje IllegalStateException, aplikacja nie powinna się zawieszać, jeśli wystąpił błąd w hoście metody(). –

+0

aplikacja nie rzuca pamięci podręcznej. – Blackbelt