嗨,我一次又一次地收到这个错误
流程:pandaboo.run.carromking,PID:30965java.lang.RuntimeException:无法实例化活动ComponentInfo{pandaboo.run.carromking/pandaboo.reun.carrom king.MainActivity}:java.lang.InstructionException:java.lang.Class<pandaboo.run.carromking.MainActivity>没有零参数构造函数
android清单如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:fullBackupContent="@xml/backup_descriptor"
tools:ignore="AllowBackup">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
生成中没有错误,但在下的清单中有一个红色下划线。MainActivity有错误:
This class should provide a default constructor (a public constructor with no arguments) (`pandaboo.run.carromking.MainActivity`)
请帮忙。
确保MainActivity如下:
public class MainActivity extends Activity {
@SuppressLint("StaticFieldLeak")
static Context context;
public final ThreadLocal<Discovery> disc;
{
disc = new ThreadLocal<Discovery>() {
@Override
protected Discovery initialValue() {
return new Discovery(Discovery.DiscoveryType.MDNS);
}
};
}
public static Publisher gamePublisher;
Subscriber gameSubscriber;
Thread testPublishing;
RenderThread renderThread;
private final Node node;
public MainActivity(Node node) {
this.node = node;
}
public class TestPublishing extends Application implements Runnable {
@Override
public void run() {
while (gamePublisher != null) {
Log.v("CarromGame: umundo", "run");
//gamePublisher.send(message.getBytes());
try {
Thread.sleep(1000);
Log.v("CarromGame:", "sleep");
} catch (InterruptedException e) {
Log.v("CarromGame: exception", "in run");
e.printStackTrace();
}
pandaboo.run.carromking.MainActivity.this.runOnUiThread(() -> {
//tv.setText(tv.getText() + "o");
Log.v("CarromGame: umundo", "context view o");
//contentView = true;
renderThread = new RenderThread(new pandaboo.run.carromking.MainActivity.displayComponents().getHolder(), new MainGamePanel(pandaboo.run.carromking.MainActivity.this));
renderThread.start();
});
}
}
}
public class TestReceiver extends Receiver {
byte[] msgb;
String type = null;
public void receive(Message msg) {
msgb = msg.getData();
type = msg.getMeta("CLASS");
Log.v("CarromGame:umundo value", "TYPE = " + type);
for (String key : msg.getMeta().keySet()) {
Log.v("CarromGame: umundo", key + ": " + msg.getMeta(key) + " value for class" + msg.getMeta("CLASS"));
}
pandaboo.run.carromking.MainActivity.this.runOnUiThread(() -> {
//tv.setText(tv.getText() + "i");
Log.v("CarromGame: umundo", "context view i before");
ObjectInputStream is = null;
if ((type != null) && (!type.equals(""))) {
try {
type = null;
ByteArrayInputStream in = new ByteArrayInputStream(msgb);
is = new ObjectInputStream(in);
is.readObject();
Log.v("CarromGame: umundo", "inside try block " + is.toString());
} catch (Exception e) {
e.printStackTrace();
}
assert is != null;
Log.v("CarromGame: umundo", "context view i after" + is.toString());
renderThread = new RenderThread(new pandaboo.run.carromking.MainActivity.displayComponents().getHolder(), new MainGamePanel(pandaboo.run.carromking.MainActivity.this));
renderThread.start();
}
});
}
}
public class displayComponents extends SurfaceView implements SurfaceHolder.Callback {
public displayComponents() {
super(getApplicationContext());
this.getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
renderThread.running = true;
renderThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
while (true) {
try {
renderThread.join();
break;
} catch (InterruptedException ignored) {
}
}
}
}
//Called when activity is created for the first time
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// To check if there is any active Wifi connection
if (!wifi.isWifiEnabled()) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
//Allow the application to receive Wifi Multicast packets.
WifiManager.MulticastLock mcLock = wifi.createMulticastLock("gameLock");
mcLock.acquire();
System.loadLibrary("umundoNativeJava");
Objects.requireNonNull(disc.get()).add(node);
Log.v("CarromGame:", "on create");
gamePublisher = new Publisher("Carrom"); //Carrom: channel Name
node.addPublisher(gamePublisher);
//gamePublisher.send();
gameSubscriber = new Subscriber("Carrom", new pandaboo.run.carromking.MainActivity.TestReceiver());
node.addSubscriber(gameSubscriber);
testPublishing = new Thread(new pandaboo.run.carromking.MainActivity.TestPublishing());
//contentView();
testPublishing.start();
Log.v("CarromGame: umundo", "inside context view");
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//setTitle(title);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
MainGamePanel.PANEL_HEIGHT = this.getWindowManager().getDefaultDisplay().getHeight();
MainGamePanel.PANEL_WIDTH = this.getWindowManager().getDefaultDisplay().getWidth();
setContentView(new MainGamePanel(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_carrom_game_umundo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
}
您不能这样做:
public MainActivity(Node node) {
this.node = node;
}
不要为Activity
声明任何构造函数。只有Android框架可以实例化Activity
,并且不能定义任何其他构造函数。