我创建了一个简单的Android应用程序,可以打开其他内置活动,如联系人,地图,电话等。通过我的应用程序,我可以打开联系人和浏览器活动,但每当我尝试打开电话或地图活动时,应用程序自动崩溃。
以下是我的MainActivity.java的代码
package com.suraj021.shortcuts;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button b1, b2, b3, b4;
int request_code= 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//******** Call Button *********//
b1= (Button) findViewById(R.id.call_btn);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("+919999999999"));
startActivity(i);
}
});
//******** Browser *********//
b3= (Button) findViewById(R.id.browser_btn);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new
Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(i);
}
});
//********* Maps ************//
b4= (Button) findViewById(R.id.maps_btn);
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:37.827500,-122.481670"));
startActivity(i);
}
});
//******* Contact Button *******//
b2= (Button) findViewById(R.id.contact_btn);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i, request_code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == this.request_code){
if(resultCode== RESULT_OK){
Toast.makeText(this, data.getData().toString(),
Toast.LENGTH_SHORT).show() ;
Intent i= new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的activity_main.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/call_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/call"/>
<Button android:id="@+id/contact_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/contact"/>
<Button android:id="@+id/browser_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/browser"/>
<Button android:id="@+id/maps_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/maps"/>
</LinearLayout>
logcat在这里:
01-15 13:50:00.605:W / dalvikvm(787):threadid = 1:线程退出未捕获的异常
(组= 0x40a71930)
01-15 13:50:00.635:E / AndroidRuntime(787):致命异常:主要
01-15 13:50:00.635:E / AndroidRuntime(787):android.content.ActivityNotFoundException:否
找到处理Intent的活动{act = android.intent.action.DIAL
dat = + 919999999999}
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Activity.startActivityForResult(Activity.java:3370)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Activity.startActivityForResult(Activity.java:3331)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Activity.startActivity(Activity.java:3566)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.Activity.startActivity(Activity.java:3534)
01-15 13:50:00.635:E / AndroidRuntime(787):at
com.suraj021.shortcuts.MainActivity $ 1.onClick(MainActivity.java:32)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.view.View.performClick(View.java:4204)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.view.View $ PerformClick.run(View.java:17355)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.os.Handler.handleCallback(Handler.java:725)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.os.Handler.dispatchMessage(Handler.java:92)
01-15 13:50:00.635:E / AndroidRuntime(787):在android.os.Looper.loop(Looper.java:137)
01-15 13:50:00.635:E / AndroidRuntime(787):at
android.app.ActivityThread.main(ActivityThread.java:5041)
01-15 13:50:00.635:E / AndroidRuntime(787):at java.lang.reflect.Method.invokeNative(Native
方法)
01-15 13:50:00.635:E / AndroidRuntime(787):at
java.lang.reflect.Method.invoke(Method.java:511)
01-15 13:50:00.635:E / AndroidRuntime(787):at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 13:50:00.635:E / AndroidRuntime(787):at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 13:50:00.635:E / AndroidRuntime(787):at dalvik.system.NativeStart.main(Native Method)
01-15 13:50:02.525:I / Process(787):发送信号。 PID:787 SIG:9
这是我的androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.suraj021.shortcuts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
在您的代码中,您将空字符串传递给Uri.parse(“”)以进行调用和映射,因此它会抛出NullPointerException。
将代码更改为
//******** Call Button *********//
b1= (Button) findViewById(R.id.call_btn);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("123456789")); //Your tel no
startActivity(i);
}
});
和地图相同。在Map中,您还必须传递非空值。