活动不会实例化

时间:2012-03-27 17:21:43

标签: java android android-layout android-listview

我已经添加了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

1 个答案:

答案 0 :(得分:3)

有一个构造函数:

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

删除它。首先,永远不会被召唤。其次,它阻止您的活动被实例化。 Android将使用零参数构造函数,而您没有定义。