这是我的两个活动的代码
此应用程序通过单击活动A的其中一个按钮,在活动B中应显示一个列表视图,其中包含活动A中命令开关(v.getId())中指定的名称,如果单击一个这些名称应始终保留下面的URL:在活动A的相同命令中
但是当我点击其中一个按钮时,应用程序崩溃,我不知道如何解决
活动A
public class GruppiPuntateActivity extends ActionBarActivity implements OnClickListener {
ArrayList<String> bottone;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.gruppipuntate_activity);
//rimozione action bar
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide();
b1 = (Button) findViewById (R.id.button1);
b2 = (Button) findViewById (R.id.button2);
b3 = (Button) findViewById (R.id.button3);
b4 = (Button) findViewById (R.id.button4);
b5 = (Button) findViewById (R.id.button5);
b6 = (Button) findViewById (R.id.button6);
b7 = (Button) findViewById (R.id.button7);
b8 = (Button) findViewById (R.id.button8);
b9 = (Button) findViewById (R.id.button9);
b10 = (Button) findViewById (R.id.button10);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
}
}
//gestione Switch java per selezione puntate
public void onClick(View v) {
String[] product;
String[] urls;
Intent episodi = new Intent(GruppiPuntateActivity.this, EpisodiActivity.class);
switch(v.getId()){
case R.id.button1:
product = new String[]{"ciao", "1", "bottone"};
urls = new String[] {"http://www.dailymotion.com/video/x21cxmf_camera-cafe-3-stagione-ep-252-un-cane-per-amico_shortfilms, url2, url3"};
episodi.putExtra("Product", product);
episodi.putExtra("urls", urls);
startActivity(episodi);
break;
case R.id.button2:
product = new String[]{"ciao", "1", "bottone"};
urls = new String[] {"url1, http://www.dailymotion.com/video/x21cxmf_camera-cafe-3-stagione-ep-252-un-cane-per-amico_shortfilms, url3"};
episodi.putExtra("Product", product);
episodi.putExtra("urls", urls);
startActivity(episodi);
break;
//etc.etc....
}
}
}
活动B
public class EpisodiActivity extends Activity {
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
public class ViewModel {
private String url;
private String name;
public ViewModel(String url, String name) {
this.url = url;
this.name = name;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
ListView mylist = (ListView) findViewById(R.id.listView1);
// And in this loop we create the ViewModel instances from
// the name and url and add them all to a List
List<ViewModel> models = new ArrayList<ViewModel>();
for (int i = 0; i < episodi.length; i++) {
String name = episodi[i];
String url = urls[i];
ViewModel model = new ViewModel(name, url);
models.add(model);
}
// Here we create the ArrayAdapter and assign it to the ListView
// We pass the List of ViewModel instances into the ArrayAdapter
final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Here we get the ViewModel at the given position
ViewModel model = (ViewModel) arg0.getItemAtPosition(position);
// And the url from the ViewModel
String url = model.getUrl();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
}
}
答案 0 :(得分:0)
if (Build.VERSION.SDK_INT < 11)
所有按钮仅针对Api级别低于11
的设备进行初始化如果您点击设备上的任何按钮,APi等级11及以上,则会收到错误。
更新:
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide();
}
// Make Button initialization outside if loop and your app will work
答案 1 :(得分:0)
Firts,你应该总是查看你的LogCat,看看会抛出什么错误。
其次,在你的按钮上,你有
//rimozione action bar
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide();
b1 = (Button) findViewById (R.id.button1);
b2 = (Button) findViewById (R.id.button2);
...
b1.setOnclickListener(this);
b2.setOnClickListener(this);
...
}
代码运行olny,当SDK小于11时。想要每次都按下按钮,所以这样做
//rimozione action bar
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide();
}
b1 = (Button) findViewById (R.id.button1);
b2 = (Button) findViewById (R.id.button2);
...
b1.setOnclickListener(this);
b2.setOnClickListener(this);
...
然后,在按钮上,zou调用setOnClickListener,它允许您处理单击事件。但是你必须传递一个新的OnClickListener对象,让它工作。看一下官方的例子here。
在你的情况下,它会是这样的:
b1.setOnclickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
我希望这会有所帮助。
答案 2 :(得分:0)
此声明
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
很糟糕,因为你还没有加载活动来从中获取信息,你需要在onCreate(),onResume或onStart()方法之后初始化这些变量
String[] episodi;//Correction
String[] urls;//Correction
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
episodi = getIntent().getStringArrayExtra("Product"); ////Line added
urls = getIntent().getStringArrayExtra("urls");////Line added
ListView mylist = (ListView) findViewById(R.id.listView1);
.......
}
现在,您可以做的最好的事情是验证此变量
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
episodi = getIntent().getStringArrayExtra("Product"); ////Line added
urls = getIntent().getStringArrayExtra("urls");////Line added
if (episodi == null) { ////Validation
Log.d("NULL", "episodi is null");
return;
}
if (urls == null) { ////Validation
Log.d("NULL", "urls is null");
return;
]
ListView mylist = (ListView) findViewById(R.id.listView1);
.......
}
答案 3 :(得分:0)
将这些行放在onCreate方法
中String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");