0

我想在 ma Galaxy S2 中运行该应用程序,但我发现了这个错误。我发布了logcat。请告诉我有什么问题?以及如何解决?谢谢

05-07 11:23:38.820: D/AndroidRuntime(8900): Shutting down VM
05-07 11:23:38.820: W/dalvikvm(8900): threadid=1: thread exiting with uncaught exception (group=0x40c3f1f8)
05-07 11:23:38.820: E/AndroidRuntime(8900): FATAL EXCEPTION: main
05-07 11:23:38.820: E/AndroidRuntime(8900): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aaaaa/com.example.aaaaa.MainActivity}: java.lang.NullPointerException
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1892)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.os.Looper.loop(Looper.java:137)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread.main(ActivityThread.java:4507)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at dalvik.system.NativeStart.main(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900): Caused by: java.lang.NullPointerException
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.view.View.<init>(View.java:2721)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.view.View.<init>(View.java:2769)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.widget.TextView.<init>(TextView.java:573)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.widget.TextView.<init>(TextView.java:566)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.widget.TextView.<init>(TextView.java:561)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at com.example.aaaaa.MainActivity.<init>(MainActivity.java:28)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at java.lang.Class.newInstanceImpl(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at java.lang.Class.newInstance(Class.java:1319)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
05-07 11:23:38.820: E/AndroidRuntime(8900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1883)
05-07 11:23:38.820: E/AndroidRuntime(8900):     ... 11 more

这是 MainActivity.java 我认为错误在于TextView view = new TextView(this);因为我在 MainActivity 中没有 TextView 但我想从另一个类的 textview 中提取数据(setting.java)

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {

       String num;
       TextView view = new TextView(this);


      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          Bundle extra = getIntent().getExtras();
          num = extra.getString("label");



          final TextView view = new TextView(this);
          Uri uriSMSURI = Uri.parse("content://sms/inbox");
          Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
          String sms = "";
          while (cur.moveToNext()) {

              String spn ="+21629188153";
              String phNum =cur.getString(2);
              if (spn.equals(phNum)){
              sms +=cur.getString(12); 
                              }      
              }

             String a =sms.substring(0,10);
             String b =sms.substring(11,21); 

              MapView maMap;
            MapController monControler;

               maMap = (MapView)findViewById(R.id.myGmap);
                maMap.setBuiltInZoomControls(true);

                GeoPoint point = new GeoPoint (microdegrees(Double.parseDouble(a)),microdegrees(Double.parseDouble(b)));

                MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.marker_voisinage_rouge));
                object.addPoint(point);
                maMap.getOverlays().add(object);
                maMap.setSatellite(true);

                monControler = maMap.getController();
                monControler.setZoom(12);
                monControler.setCenter(point);

                Button monBouton = (Button)findViewById(R.id.envoie_sms);

                monBouton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        View monBoutton = null;
                        // TODO Auto-generated method stub

                            Intent monIntent = new Intent(MainActivity.this,SendSms.class);
                            startActivity(monIntent);

                    }
                } );

                Button mnBouton = (Button)findViewById(R.id.setting);

                mnBouton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        View monBoutton = null;
                        // TODO Auto-generated method stub

                        setContentView(R.layout.setting);

                    }
                } );

                Button myBouton = (Button)findViewById(R.id.aff);

                myBouton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        View monBoutton = null;
                        // TODO Auto-generated method stub

                          view.setText(num);
                          setContentView(view);

                    }
                } );
      }










            private int microdegrees (double value){
                return (int)(value*1000000);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
            }

            @Override
            protected boolean isRouteDisplayed() {
                // TODO Auto-generated method stub
                return false;
            }
            public class MonOverlay extends ItemizedOverlay<OverlayItem>{

                List<GeoPoint> points = new ArrayList<GeoPoint>();

                public MonOverlay(Drawable arg0) {
                    super(boundCenterBottom(arg0));
                    // TODO Auto-generated constructor stub
                }

                @Override
                protected OverlayItem createItem(int i) {
                    GeoPoint point = points.get(i);
                    return new OverlayItem(point,"titre","description");
                }

                @Override
                public int size() {

                    return points.size();
                }

                public void addPoint (GeoPoint point){
                    this.points.add(point);
                    populate();
                }

            } 


}

这是 Setting.java,我想传递s1 = num.getText().toString();给 MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class setting extends Activity {
    Button sauvg;
    EditText    num;
    String s1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        sauvg = (Button)findViewById(R.id.ok);
        num = (EditText)findViewById(R.id.editText1);

        sauvg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                Intent t = new Intent(setting.this, MainActivity.class);
                s1 = num.getText().toString();
                t.putExtra("label",s1);
                startActivity(t);


                // TODO Auto-generated method stub

            }
        });

}
}
4

1 回答 1

2

错误在这里:

public class MainActivity extends MapActivity {

   String num;
   TextView view = new TextView(this); // <- HERE

您不能在那里创建视图,但您应该在onCreate方法中移动创建,如下所示:

公共类 MainActivity 扩展 MapActivity {

   String num;
   TextView view;


  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      this.view = new TextView(this);
      Bundle extra = getIntent().getExtras();

注意变量名:我看到你有 2 个名为“view”,这不是错误(一个是 中的局部变量onCreate,另一个是实例变量)但很容易出错。

顺便说一句,您不能直接访问其他 Activity 中的 TextView。如果你想在活动之间共享数据,你应该使用 Intents 或 Message&Handler

编辑

如果 MainActivity 是您的应用程序启动时要启动的第一个活动,我想您的应用程序会崩溃。发生这种情况是因为您应该在使用它之前检查调用意图是否有额外内容,如下所示:

Bundle extra = getIntent().getExtras();
if (extra != null) {
   num = extra.getString("label");
}
于 2013-05-07T10:53:03.300 回答