My android intents program crashes why?
I have written an android program to learn about intents.I have made the man.Xml with all needed buttons and it compiles fine. But when I load it into the emulator it crashes (Force quit).What is the reason?Also when I comments intentt2activity constructor as shown in the code,it displays the buttons.
package com.intent2;
import com.intent2.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Intent2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/* public Intent2Activity(View view) {
/*Intent intent;
switch (view.getId()) {
case R.id.Button01:
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.vogella.de"));
//startActivity(intent);
break;
case R.id.Button02:
intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:(+49)12345789"));
//startActivity(intent);
break;
case R.id.Button03:
intent = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:(+49)12345789"));
//startActivity(intent);
break;
开发者_StackOverflowcase R.id.Button04:
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:50.123,7.1434?z=19"));
//startActivity(intent);
break;
case R.id.Button05:
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=query"));
//startActivity(intent);
break;
case R.id.Button06:
intent = new Intent("android.media.action.IMAGE_CAPTURE");
//startActivityForResult(intent, 0);
break;
case R.id.Button07:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/"));
//startActivity(intent);
break;
case R.id.Button08:
intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1"));
//startActivity(intent);
break;
default:
break;
}
}*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
Toast.makeText(this, result, Toast.LENGTH_LONG);
}
}
}
If you look at the device/emulator log it will tell you what kind of exception caused the force close (and the stack trace should point you to the exact line that is responsible).
To view the log, run the following command from the command line:
adb -e logcat
Based on what you've told us, I suspect there is an exception parsing one of the URIs.
Your data
variable is probably null
.
Intent2Activity Constructor
is called before onCreate
method, so when you calls it, the content still null
consider that.
and you must use Listeners with views -->onItemClickListener
精彩评论