0

以下代码可以正常工作,没有任何错误,但是当我尝试通过意图获取 url 时,我收到应用程序意外停止的错误。谁能帮帮我。

public class MjpegSample extends Activity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String URL = "http://121.6.98.160:8081/videostream.cgi?"
                   + "user=admin&pwd=admin&resolution=8"; 
    }
}

当我替换上面的代码时,下面的代码会出错:

public class MjpegSample extends Activity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String URL;
        Bundle extras = getIntent().getExtras();
        URL = extras.getString("flink");
    }
}

为了通过意图获得“flink”,我使用以下内容

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.viewcam);
    t=(EditText)findViewById(R.id.username);
    p=(EditText)findViewById(R.id.password);
    i=(EditText)findViewById(R.id.ip);
    po=(EditText)findViewById(R.id.port);
    b1.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            String tt=t.getText().toString();
            String pp=p.getText().toString();
            String ii=i.getText().toString();
            String por=po.getText().toString();
            String link="http://"+ii+":"+por
                       +"/videostream.cgi?user="+tt
                       +"&pwd="+pp+"resolution=8";
            Intent myIntent = new Intent(v.getContext(), MjpegSample.class);
            myIntent.putExtra("flink",link);
            startActivityForResult(myIntent, 0);                

        }
    });
}
4

2 回答 2

0

尝试替换v.getContext()NameOfCurrentActivity.this,同时检查其他活动:

if (extras != null)
{
    // do your thing
}
于 2012-04-19T05:39:54.633 回答
0

您应该直接从意图中获取 url,而不是从附加内容中获取:

getIntent().getString("flink")
于 2012-04-19T05:40:54.210 回答