【发布时间】:2014-08-08 17:15:32
【问题描述】:
这是我的两个活动的代码
此应用程序通过单击活动 A 的按钮之一,在活动 B 中应显示一个列表视图,其名称在活动 A 中的命令开关 (v.getId()) 中指定,如果单击一个这些名称中应始终保留以下 url:在 Activity 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)));
}
});
}
}
【问题讨论】:
-
@amarildo 这是一个空指针错误
-
我们可能需要您的 logcat,因为 NullPointerException 的原因有很多。
-
我需要复制代码吗?我怎样才能给你看
-
检查我的答案,如果不起作用,请告诉我。
标签: android eclipse listview android-intent crash