gpt4 book ai didi

java - Activity 不会实例化

转载 作者:行者123 更新时间:2023-12-02 00:26:42 25 4
gpt4 key购买 nike

我已经添加了 ArrayAdapter 的功能,但是该 Activity 现在不会实例化。在我为 ListView 添加 ArrayAdapter 并且 ChatActivity 位于 list 中之前,一切都运行良好。我已经尝试解决这个问题有一段时间了,我确信它很简单,我只是看不到它。

这是代码。

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

最佳答案

确实有一个构造函数:

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

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

关于java - Activity 不会实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9894476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com