【问题标题】:Crash starting new activity? (Intent)开始新活动时崩溃? (意图)
【发布时间】: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


【解决方案1】:

此声明

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

【讨论】:

  • 是你的代码XD,到最后我把“点”说“方法没有完成”
  • :) 这部分 if (episodi == null) { ////Validation Log.d("NULL", "episodi is null");返回; } if (urls == null) { ////Validation Log.d("NULL", "urls is null");返回;
  • 如果您没有在 getIntent().getStringArrayExtra("Product"); 中传递数据或 getIntent().getStringArrayExtra("urls");如果你想评论 //return;没问题,但如果变量为空,你的应用程序就会崩溃
  • 如何查看它们是否传递数据?
  • 结果在logcat中看到了吗?
【解决方案2】:
  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

【讨论】:

    【解决方案3】:

    首先,您应该始终查看您的 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
       }
    });
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      将这些行放在 onCreate 方法中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-03
        • 1970-01-01
        • 2018-07-31
        • 1970-01-01
        相关资源
        最近更新 更多