我正在尝试从我设置的主要活动组中设置启动子活动。
我不习惯使用活动组,因此我遇到了一些麻烦。
我设置了4个单选按钮,我希望每个单元都能从我的MainActivity中打开一个子Activity。
我收到错误声明" OpenCategories无法解析为类型"和"方法startChildActivity(String,Intent)未定义类别Categories"
我知道我在这里做错了所以需要一些关于设置的指导。
主要活动:
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends ActivityGroup {
private ArrayList<String> mIdList;
GridView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
RadioButton btnAll = (RadioButton) findViewById(R.id.btnAll);
btnAll.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
RadioButton btnCategories = (RadioButton) findViewById(R.id.btnCategories);
btnCategories
.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
RadioButton btnPopular = (RadioButton) findViewById(R.id.btnPopular);
btnPopular
.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
RadioButton btnAbout = (RadioButton) findViewById(R.id.btnAbout);
btnAbout.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
}
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
private CompoundButton.OnCheckedChangeListener btnAllOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
}
}
};
private CompoundButton.OnCheckedChangeListener btnCategoriesOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
//Getting error here
Intent OpenCategories = new Intent(getParent(), Categories.class);
OpenCategories.putExtra("Flag", 1);
OpenCategories = (OpenCategories)getParent();
Categories.startChildActivity("YOUR_ID", OpenCategories);
}
}
};
private CompoundButton.OnCheckedChangeListener btnPopularOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened POPULAR tab",
Toast.LENGTH_SHORT).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnAboutOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened ABOUT tab",
Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
private String[] mStrings = {
"www.LOTSOFURLSHERE.com", };
};
答案 0 :(得分:1)
// Replace this code
Intent OpenCategories = new Intent(getParent(), Categories.class);
OpenCategories.putExtra("Flag", 1);
OpenCategories = (OpenCategories)getParent();
Categories.startChildActivity("YOUR_ID", OpenCategories);
To
Intent OpenCategories = new Intent(MainActivity.this, Categories.class);
OpenCategories.putExtra("Flag", 1);
startActivity(OpenCategories);