this is the code of my two activty
this app by the clicking one of the buttons of Activity A, in the Activity B should show a listview with the names specified in the command switch (v.getId ()) in the activity A, and if you click on one of those names should always leave the url below: in the same command of Activity A
But as soon as I click on one of the buttons the app crashes, i don't know how to solve
Activity 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....
}
}
}
Activity 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)));
}
});
}
}
if (Build.VERSION.SDK_INT < 11)
all your buttons are initialized only for devices with Api level less than 11
if you click any button on Device with APi level 11 and Above youll get error.
Update :
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide();
}
// Make Button initialization outside if loop and your app will work
Firts, you should always look in your LogCat, to look what error is thrown.
Second, on your buttons, you have
//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);
...
}
The code runs olny, when the SDK is smaller then 11. Want to function your buttons everytime, so do this
//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);
...
And then, on the button, zou call setOnClickListener, which allows you to handle click events. But you have to pass a new OnClickListener object, to let it work. look at the official example here .
In you case, its gonna be like this:
b1.setOnclickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
I hope this helps.
This declarations
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
Are bad because you are not loading yet the activity to get information from that, you need initialize this variables after, in your onCreate(), onResume or onStart() methods
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);
.......
}
Now the best thing that you can do is a validation for this variables
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);
.......
}
put these lines in the onCreate method
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.