2012-03-27 93 views
0

我已經添加了ArrayAdapter的功能,但活動現在不會實例化。在爲listview添加ArrayAdapter並且ChatActivity在清單中之前,所有工作都完美無缺。我一直在努力工作一段時間,並確定其簡單的東西,我只是不能看到它。活動不會實例化

這是代碼。

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佈局:

<?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> 

堆棧跟蹤:

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

你確定在這裏列出的那個下面沒有額外的「引起」節嗎?另外,由於你的代碼清單在類之前結束,你確定你沒有'ChatActivity'的構造函數嗎? – CommonsWare 2012-03-27 17:26:24

+0

沒有,這是整個班級和產生的堆棧 – EmberZ 2012-03-27 17:33:48

+0

這很奇怪。 'InstantiationException'來自像有一個構造函數的東西,或者類不公開,或者類似的東西。除非'ChatActivity'是某種內部類,否則我不會看到你錯在哪裏。 – CommonsWare 2012-03-27 17:36:02

回答

3

有一個構造函數:

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

刪除此。首先,它永遠不會被調用。其次,它阻止了你的活動被實例化。 Android將使用零參數構造函數,並且您沒有定義。

+0

我在一個小時前工作過,非常感謝反正男人! – EmberZ 2012-03-27 20:53:50

相關問題