2012-03-27 13 views
0

Ich habe die Funktionalität für den ArrayAdapter hinzugefügt, die Aktivität wird jedoch jetzt nicht instanziiert. Alles hat perfekt funktioniert, bevor ich den ArrayAdapter für die Listview hinzugefügt habe und die ChatActivity im Manifest ist. Ich habe versucht, dies für eine Weile auszuarbeiten und bin sicher, dass es etwas Einfaches ist, ich kann es einfach nicht sehen.Aktivität wird nicht instanziiert

Hier ist der Code.

import com.DrawTastic.Networking.LocalBinder; 

public class ChatActivity extends ListActivity implements Runnable, OnClickListener { 

private MyView myView; 
private Networking mService; 
private boolean mBound = false; 
PrintWriter writer; 
BufferedReader reader; 
ObjectInputStream send; 
ObjectOutputStream messSend; 
ObjectInputStream messRec; 
private EditText Text; 

private ArrayAdapter<Message> mAdapter; 
private ArrayList<Message> mMesseges = new ArrayList<Message>(); 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chat); 

    mAdapter = new MessageAdapter(this, mMesseges); 

    myView = (MyView) findViewById(R.id.MyView1); 

    setListAdapter(mAdapter); 

    Text = (EditText) findViewById(R.id.input); 

    Text.setOnClickListener(this); 

    //Thread bob = new Thread(this); 
    //bob.start(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    // Bind to LocalService 
    Intent intent = new Intent(this, Networking.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    // Unbind from the service 
    if (mBound) { 
     unbindService(mConnection); 
     mBound = false; 
    } 
} 

/** 
* Called when a button is clicked (the button in the layout file attaches 
* to this method with the android:onClick attribute) 
*/ 

/** Defines callbacks for service binding, passed to bindService() */ 
private ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     // We've bound to LocalService, cast the IBinder and get 
     // LocalService instance 
     LocalBinder binder = (LocalBinder) service; 
     mService = binder.getService(); 
     mBound = true; 
     myView.setService(mService); 
     getDrawOIS(); 
     getMessOOS(); 
     getMessOIS(); 

    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     mBound = false; 
     mService = null; 
     myView.setService(null); 
    } 
}; 

public void getDrawOIS() { 
    send = mService.getImageOis(); 
} 

public void getMessOIS() { 
    messRec = mService.getMessageOis(); 
} 

public void getMessOOS() { 
    messSend = mService.getMessageOops(); 
} 

public ChatActivity(ObjectInputStream stream) { 
    this.messRec = stream; 
} 

public void oopsMess(Message mess) { 
    try { 
     messSend.writeObject((Message) mess); 
     messSend.flush(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public void run() { 
    try { 
     while (true) { 
      while (true) { 
       Message mess = (com.DrawTastic.Message) messRec.readObject(); 
       mAdapter.add(mess); 
      } 
     } 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onClick(View v) { 
    Message mess = new Message(Text.toString(), "Username"); 
    oopsMess(mess); 
    Text.setText(null); 
} 

XML Layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" android:weightSum="1"> 


<RelativeLayout android:id="@+id/relativeLayout2" 
    android:layout_height="wrap_content" android:layout_width="match_parent" 
    android:layout_weight="0.99"> 

    <view class="com.DrawTastic.MyView" 
     android:id="@+id/MyView1" 
     android:layout_gravity="center"  
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 



</RelativeLayout> 


<RelativeLayout android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" android:layout_height="150px"> 


    <EditText android:layout_height="wrap_content" android:id="@+id/input" 
     android:layout_width="250dp" android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true"> 
     <requestFocus></requestFocus> 
    </EditText> 

    <Button android:layout_height="wrap_content" android:id="@+id/send" 
     android:layout_width="wrap_content" android:text="Send" 
     android:onClick="onClick" android:layout_alignParentBottom="true" 
     android:layout_toRightOf="@+id/input" 
     android:layout_alignParentRight="true"></Button> 


    <ListView android:id="@android:id/list" android:layout_width="match_parent" 
     android:layout_above="@+id/input" android:layout_alignParentRight="true" android:layout_height="100dp"></ListView> 
</RelativeLayout> 



</LinearLayout> 

Stack Trace:

03-27 17:12:22.835: ERROR/AndroidRuntime(364): FATAL EXCEPTION: main 
03-27 17:12:22.835: ERROR/AndroidRuntime(364): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.DrawTastic/com.DrawTastic.ChatActivity}: java.lang.InstantiationException: com.DrawTastic.ChatActivity 
    03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.os.Looper.loop(Looper.java:123) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at java.lang.reflect.Method.invoke(Method.java:507) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at dalvik.system.NativeStart.main(Native Method) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364): Caused by: java.lang.InstantiationException: com.DrawTastic.ChatActivity 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at java.lang.Class.newInstanceImpl(Native Method) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at java.lang.Class.newInstance(Class.java:1409) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 
03-27 17:12:22.835: ERROR/AndroidRuntime(364):  ... 11 more 
+0

Sind Sie sicher, dass unterhalb der hier aufgelisteten Zeile keine weitere "Caused by" -Zeile liegt? Da Ihre Code-Auflistung vor der Klasse endet, sind Sie sicher, dass Sie keinen Konstruktor für 'ChatActivity' haben? – CommonsWare

+0

Nein, das ist die gesamte Klasse und der daraus resultierende Stack – EmberZ

+0

Das ist sehr seltsam. 'InstantiationException' kommt von Sachen, die einen Konstruktor haben, oder die Klasse ist nicht öffentlich oder solche Sachen. Wenn "ChatAktivität" keine innere Klasse von etwas ist, sehe ich nicht, wo du falsch liegst. – CommonsWare

Antwort

3

Sie tun haben einen Konstruktor:

public ChatActivity(ObjectInputStream stream) { 
    this.messRec = stream; 
} 

diese löschen. Erstens wird es niemals aufgerufen werden. Zweitens verhindert es, dass Ihre Aktivität instanziiert wird. Android verwendet den Zero-Argument-Konstruktor, und Sie haben keinen definiert.

+0

Ich habe das vor einer Stunde ausgearbeitet, Mega danke sowieso Mann! – EmberZ

Verwandte Themen