簡體   English   中英

啟動新活動時崩潰? (意圖)

[英]Crash starting new activity? (Intent)

這是我的兩個活動的代碼

通過單擊活動A的按鈕之一,此應用程序將在活動B中顯示一個列表視圖,其中包含在活動A中的命令開關(v.getId())中指定的名稱,如果您單擊其中一個名稱應始終在活動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)));
        }
    });
}

}

  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

首先,您應該始終查看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);
    ...
}

當SDK小於11時,代碼將運行olny。要每次都使用按鈕,請執行此操作

//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);
...

然后,在按鈕上,調用setOnClickListener,它允許您處理單擊事件。 但是您必須傳遞一個新的OnClickListener對象,以使其工作。 這里的官方例子。

在你的情況下,它會像這樣:

b1.setOnclickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
   }
});

我希望這有幫助。

此聲明

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);
.......
    }

將這些行放在onCreate方法中

String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM