我正在尝试使AnotherActivity
扩展主FullscreenActivity
,但我发现错误,没有空的构造函数
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.terrh/com.example.terrh.FullscreenActivity$AnotherActivity}: java.lang.InstantiationException: can't instantiate class com.example.terrh.FullscreenActivity$AnotherActivity; no empty constructor
所以我试图添加它,但不正确。
FulscreenActivity.java
public class FullscreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ihkj);
}
public void buttonClicked(View button) {
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
}
public class AnotherActivity extends FullscreenActivity {
public AnotherActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
}
}
}
的Manifest.xml
<activity
android:name="com.example.terrh.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.terrh.FullscreenActivity$AnotherActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter >
<action android:name="in.wptrafficanalyzer.AnotherActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
如何正确添加空构造函数以使其正常工作,还是可以用其他方式解决此问题?
答案 0 :(得分:0)
首先,当不需要扩展Activity时,你无法创建默认构造函数。如果要获取anotherActivity类的公共函数,则创建该类的静态方法getInstance()。见下面的代码:
public static Activity activity;
public void onCreate (...){
....
....
activity=anotherActivity.this;
}
public static anotherActivity getInstance(){
return activity;
}
多数民众赞成......