启动第二个活动时出错

时间:2011-05-30 21:30:48

标签: android

当我点击下一个按钮时,它会强行关闭;

 05-30 23:46:21.595: VERBOSE/AudioHardwareMSM72XX(95): open driver
        05-30 23:46:21.775: ERROR/AndroidRuntime(3401): FATAL EXCEPTION: main
 05-30 23:46:21.775: ERROR/AndroidRuntime(3401): java.lang.RuntimeException: Unable to start activity ComponentInfo{yaraby.y/yaraby.y.yarab}: java.lang.NullPointerException
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
     05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.os.Handler.dispatchMessage(Handler.java:99)
 05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.os.Looper.loop(Looper.java:123)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread.main(ActivityThread.java:4633)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at java.lang.reflect.Method.invoke(Method.java:521)
  05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at co m.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at dalvik.system.NativeStart.main(Native Method)
   05-30 23:46:21.775: ERROR/AndroidRuntime(3401): Caused by: java.lang.NullPointerException
   05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at yaraby.y.yarab.onCreate(yarab.java:92)
   05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
  05-30 23:46:21.775: ERROR/AndroidRuntime(3401):     ... 11 more

Yaraby代码

     public class yaraby extends Activity {
    /** Called when the activity is first created. */
    String sentence,sentence1, modifiedSentence;;
    EditText inputfld, txt;
    TextView Text;
    Button Enter, Connect, next;
    Socket clientSocket;

    DataOutputStream outToServer;
    BufferedReader inFromServer ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inputfld=(EditText)findViewById(R.id.input);
        Text=(TextView)findViewById(R.id.view);
        Enter=(Button)findViewById(R.id.enter);
        Connect=(Button)findViewById(R.id.connect);
//        yaraby x=new yaraby();
        Text.append( "\n about to create a socket");    


        txt=(EditText)findViewById(R.id.txt);
        next = (Button) findViewById(R.id.Button01);   

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    Text.append("PPPPP");
                Intent i = new Intent(yaraby.this, yarab.class);

                startActivity(i);
                Text.append("OPS");
                }
                catch(Error e){
                    Log.e("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", e.getMessage());
                    Text.append("ofa7ay");
                }

            }});

                      }

    public void appendText(View view){
        Text.append( "\n  In chat ");

        try{
    //  while (true)
        //{

        sentence=inputfld.getText().toString();
        sentence1="ingyyyy";
        Text.append( "\n  DataStream creating");        
        outToServer.writeBytes(sentence);
        outToServer.writeBytes(sentence1);
        Thread.sleep(1000);
        modifiedSentence = inFromServer.readLine(); 
        Text.append(modifiedSentence);
        inputfld.setText(null);
        //if(modifiedSentence.equals("QUIT"))
        //break;
   //   }
        }
        catch(Exception modifiedsentence){
            Text.append("Exception");
        }


    }


    public void connect(View view){
        try{
            clientSocket= new Socket("192.168.1.6",54611); 
            Text.append( "\n created a socket");
            outToServer = new DataOutputStream(clientSocket.getOutputStream());
            Text.append( "\n created a datastream");
            inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            Text.append( "\n created a bufferReader");
            outToServer = new DataOutputStream(clientSocket.getOutputStream());


        }catch(Exception e){
            System.out.println("Error");
        }
         }


        }

yarab code

public class yarab extends MapActivity

{
    Socket clientSocket;
    TextView Text; 
    private MapView mapView;
    private MapController mc;
    DataOutputStream outToServer;            
    BufferedReader inFromServer ;
 Button start;
    int error = 50;
    GeoPoint p, p2;
    List<Overlay> mapOverlays;
    Drawable drawable, drawable2;
    HelloItemizedOverlay itemizedOverlay, itemizedOverlay2;
    LocationManager locationManager;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState)

    {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            /* Use the LocationManager class to obtain GPS locations */

            LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            LocationListener mlocListener = new MyLocationListener();

            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, mlocListener);
            mapView = (MapView) findViewById(R.id.mapView);

            // enable Street view by default
            mapView.setStreetView(true);

            // enable to show Satellite view
            mapView.setSatellite(true);

            // enable to show Traffic on map
            // mapView.setTraffic(true);
            mapView.setBuiltInZoomControls(true);
        //    start=(Button)findViewById(R.id.start);





            mc = mapView.getController();
            // mapView.setStreetView(true);
            // mapView.setSatellite(true);
            mc.setZoom(12);
            addOverLays();
//            start= (Button) findViewById(R.id.start);   

                start.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try{
                            Text.append("oogPPP");
                        Intent i = new Intent(yarab.this, yaraby.class);

                        startActivity(i);
                        Text.append("OwwwwPS");
                        }
                        catch(Error e){
                          e.setStackTrace(null);
                        }

                    }});

                              }








    public void addOverLays() {
        String[] coordinates = { "31.216487288475037", "29.932637214660645",
                "30.084123015403748", "51.5002", "-0.1262";}
        double lat = 29.98739718380868, lat2 = 29.987810254096985;
        double log = 31.442527770886084, log2 = 31.44258677959442;
        p = new GeoPoint((int) (lat * 1E6), (int) (log * 1E6));
        p2 = new GeoPoint((int) (lat2 * 1E6 ), (int) (log2 * 1E6 ));

        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.wit);
        drawable2 = this.getResources().getDrawable(R.drawable.ballon);

        itemizedOverlay = new HelloItemizedOverlay(drawable, this);
        itemizedOverlay2 = new HelloItemizedOverlay(drawable2, this);

        OverlayItem overlayitem = new OverlayItem(p, "Cairo", " over1");
        OverlayItem over2 = new OverlayItem(p2, "ulm", "over2");


        itemizedOverlay.addOverlay(overlayitem);

        mapOverlays.add(itemizedOverlay);

        itemizedOverlay2.addOverlay(over2);
        mapOverlays.add(itemizedOverlay2);


        mc.setZoom(17);
    //  mc.animateTo(p19);


    }






    public void Location(){



        LocationManager myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


        LocationListener mlocListener1 = new MyLocationListener();

        LocationManager mlocManager1 = null;
        mlocManager1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, mlocListener1);
            //  Get the current location in start-up
              GeoPoint initGeoPoint = new GeoPoint((int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
           .getLatitude()*1000000),(int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));
           mc.animateTo(initGeoPoint);
        mc.setZoom(16);

        drawable21 = this.getResources().getDrawable(R.drawable.ggg);
        itemizedOverlay21 = new HelloItemizedOverlay(drawable21,this);
        OverlayItem over21 = new OverlayItem(initGeoPoint, "offff", "over21");

        itemizedOverlay21.addOverlay(over21);
        mapOverlays.add(itemizedOverlay21);
    }


    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener

    {

        @Override
        public void onLocationChanged(Location loc)

        {

            GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6),
                    (int) (loc.getLongitude() * 1E6));



            String Text = "My current location is: " +

            "Latitud =" + loc.getLatitude()+ 

            "Longitud =" +loc.getLongitude() ;// loc.getLongitude() ; //loc.getLatitude()+"anoog" + (p3.getLatitudeE6() - error)

            Toast.makeText(getApplicationContext(),

            Text,

            Toast.LENGTH_SHORT).show();


        mc.animateTo(point);

        }


        private Resources getResources() {
            // TODO Auto-generated method stub
            return null;
        }


        public void onProviderDisabled(String provider)

        {

            Toast.makeText(getApplicationContext(),

            "Gps Disabled",

            Toast.LENGTH_SHORT).show();

        }

        public void onProviderEnabled(String provider)

        {

            Toast.makeText(getApplicationContext(),

            "Gps Enabled",

            Toast.LENGTH_SHORT).show();

        }

        public void onStatusChanged(String provider, int status, Bundle extras)

        {

        }

        protected boolean isRouteDisplayed() {
            return false;
        }

    }/* End of Class MyLocationListener */

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

            }

2 个答案:

答案 0 :(得分:0)

内部<application>标记。有关AndroidManifest.xml结构的详细说明,请参阅here


<manifest ...>
    <application ...>
        <activity ... >
         ...
        </activity>
        ...
        <uses-library android:name="com.google.android.maps" />
    </application>
</manifest>

答案 1 :(得分:0)

yarab

1)start按钮永远不会被初始化,您尝试为它设置一个监听器。这将导致nullPointer。你上面有一条注释行 - 它会初始化它,但我不知道你为什么评论它。

2)即使修复了这个内容,你也会尝试追加Text,而Text又未被初始化。您在yaraby类中初始化{{1}},但请记住它们是不同的变量。

这些只是我注意到的问题。我的建议?尝试熟悉日志。你的logcat说你有一个由yarab.java中的第92行引起的nullPointerException,但你没有提到哪一行是非常有帮助的。