java.lang.NullPointerException at com.example.phone.MainActivity.onCreate

写个android拨号器,运行是报如下错误

  04-09 01:36:33.880: E/AndroidRuntime(3689): Caused by: java.lang.NullPointerException
04-09 01:36:33.880: E/AndroidRuntime(3689):     at com.example.phone.MainActivity.onCreate(MainActivity.java:30)


网上找了很久也没有找到答案,请高手指点下

代码如下:

1 package com.example.phone;

  2
  3 import android.support.v7.app.ActionBarActivity;
  4 import android.support.v7.app.ActionBar;
  5 import android.support.v4.app.Fragment;
  6 import android.text.TextUtils;
  7 import android.content.Intent;
  8 import android.net.Uri;
  9 import android.os.Bundle;
 10 import android.view.LayoutInflater;
 11 import android.view.Menu;
 12 import android.view.MenuItem;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.view.View.OnClickListener;
 16 import android.widget.Button;
 17 import android.widget.EditText;
 18 import android.widget.Toast;
 19 import android.os.Build;
 20
 21 public class MainActivity extends ActionBarActivity {
 22         public EditText call_number;
 23
 24         @Override
 25         protected void onCreate(Bundle savedInstanceState) {
 26                 super.onCreate(savedInstanceState);
 27                 setContentView(R.layout.activity_main);
 28                 Button bt_call = (Button) findViewById(R.id.button);
 29                 call_number = (EditText) this.findViewById(R.id.et_number);
 30                 bt_call.setOnClickListener(new mybuttonclick());
 31                 if (savedInstanceState == null) {
 32                         getSupportFragmentManager().beginTransaction()
 33                                         .add(R.id.container, new PlaceholderFragment()).commit();
 34                 }
 35         }
 36
 37         private class mybuttonclick implements OnClickListener {
 38
 39                 @Override

40                 public void onClick(View v) {
 41                         String number = call_number.getText().toString().trim();
 42                         if (TextUtils.isEmpty(number)) {
 43                                 Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT)
 44                                                 .show();
 45                                 return;
 46                         }
 47                         Intent intent = new Intent();
 48                         intent.setAction(Intent.ACTION_CALL);
 49                         intent.setData(Uri.parse("tel:" + number));
 50                         startActivity(intent);
 51                 }
 52
 53         }
 54
 55         /**
 56          * A placeholder fragment containing a simple view.
 57          */
 58         public static class PlaceholderFragment extends Fragment {
 59
 60                 public PlaceholderFragment() {
 61                 }
 62
 63                 @Override
 64                 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 65                                 Bundle savedInstanceState) {
 66                         View rootView = inflater.inflate(R.layout.fragment_main, container,
 67                                         false);
 68                         return rootView;
 69                 }
 70         }
 71
 72 }



运行时报如下错误,求助


04-09 01:36:33.880: W/dalvikvm(3689): threadid=1: thread exiting with uncaught exception (group=0x40bcc300)
04-09 01:36:33.880: E/AndroidRuntime(3689): FATAL EXCEPTION: main
04-09 01:36:33.880: E/AndroidRuntime(3689): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.phone/com.example.phone.MainActivity}: java.lang.NullPointerException
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread.access$700(ActivityThread.java:131)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.os.Looper.loop(Looper.java:137)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread.main(ActivityThread.java:4866)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at dalvik.system.NativeStart.main(Native Method)
04-09 01:36:33.880: E/AndroidRuntime(3689): Caused by: java.lang.NullPointerException
04-09 01:36:33.880: E/AndroidRuntime(3689):     at com.example.phone.MainActivity.onCreate(MainActivity.java:30)

04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.Activity.performCreate(Activity.java:5143)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-09 01:36:33.880: E/AndroidRuntime(3689):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-09 01:36:33.880: E/AndroidRuntime(3689):     ... 11 more


你可能感兴趣的:(android)