大家好我是Android的新手,我正试图从我的应用程序拨打一个示例号码。用于进行调用的代码片段代码非常简单但我不知道为什么如果我的代码片段在主要活动的方法中所有它运行良好,但如果它在另一个活动中它给我NullPointerException。这是我的片段:
public void prova3(){
try{
String url2 = "tel:33334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url2));
startActivity(intent);
}catch(Exception e){
e.printStackTrace();
}
}
Logcat:
10-10 12:12:54.566: W/System.err(30802): java.lang.NullPointerException
10-10 12:12:54.571: W/System.err(30802): at android.app.Activity.startActivityForResult(Activity.java:3532)
10-10 12:12:54.571: W/System.err(30802): at android.app.Activity.startActivityForResult(Activity.java:3493)
10-10 12:12:54.571: W/System.err(30802): at android.app.Activity.startActivity(Activity.java:3735)
10-10 12:12:54.571: W/System.err(30802): at android.app.Activity.startActivity(Activity.java:3703)
10-10 12:12:54.571: W/System.err(30802): at com.example.findmyclients.Hide_Unhide.prova3(Hide_Unhide.java:162)
10-10 12:12:54.571: W/System.err(30802): at com.example.findmyclients.Hide_Unhide$1.onClick(Hide_Unhide.java:55)
10-10 12:12:54.571: W/System.err(30802): at android.view.View.performClick(View.java:4654)
10-10 12:12:54.571: W/System.err(30802): at android.view.View$PerformClick.run(View.java:19438)
10-10 12:12:54.571: W/System.err(30802): at android.os.Handler.handleCallback(Handler.java:733)
10-10 12:12:54.571: W/System.err(30802): at android.os.Handler.dispatchMessage(Handler.java:95)
10-10 12:12:54.576: W/System.err(30802): at android.os.Looper.loop(Looper.java:146)
10-10 12:12:54.576: W/System.err(30802): at android.app.ActivityThread.main(ActivityThread.java:5602)
10-10 12:12:54.576: W/System.err(30802): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:12:54.576: W/System.err(30802): at java.lang.reflect.Method.invoke(Method.java:515)
10-10 12:12:54.576: W/System.err(30802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-10 12:12:54.576: W/System.err(30802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-10 12:12:54.576: W/System.err(30802): at dalvik.system.NativeStart.main(Native Method)
和对话框中按钮的方法:
package com.example.findmyclients;
import com.google.android.gms.internal.v;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
public class Hide_Unhide extends Activity {
LinearLayout barrainfo;
ImageButton icotelefono;
ImageButton icoemail;
ImageButton icofacebook;
ImageButton icotwitter;
ImageButton icosito;
ImageButton prova;
public Hide_Unhide (View v){
barrainfo = (LinearLayout)v.findViewById(R.id.links_bar); //tutta la barra
icotelefono = (ImageButton) v.findViewById(R.id.telephone_icon); //pulsante chiamata
icoemail = (ImageButton) v.findViewById(R.id.email_icon); //pulsante nuova mail
icofacebook = (ImageButton) v.findViewById(R.id.facebook_icon); //pulsante facebook
icotwitter = (ImageButton) v.findViewById(R.id.twitter_icon); //pulsante twitter
icosito = (ImageButton) v.findViewById(R.id.sito_icon); //pulsante sito web
/* * * * * Try share button * * * */
/*******************************/
ImageButton trysharebutton = (ImageButton) v.findViewById(R.id.share_icon);
trysharebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
prova3();
}
});
/*******************************/
方法prova3();显然是在对话的同一类。 谢谢