Android 错误:android.widget.TextView.<init>

Posted

技术标签:

【中文标题】Android 错误:android.widget.TextView.<init>【英文标题】:Android Error: android.widget.TextView.<init> 【发布时间】:2013-05-01 06:36:30 【问题描述】:

我想在 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 ComponentInfocom.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,但我想从另一个类 (setting.java) 中的 textview 中提取数据

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

            
        );



【问题讨论】:

能否提供MainActivity代码? 我编辑了问题 MainActivity 的第 28 行发生了什么?您正在将 null 传递给方法 对不起,我没听懂你的意思。请给我解释一下 【参考方案1】:

错误在这里:

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 是您的应用启动时要启动的第一个 Activity,我想您的应用会崩溃。发生这种情况是因为您应该在使用前检查调用意图是否有额外内容,如下所示:

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

【讨论】:

它对我有帮助,但我无法从其他类中提取我的 cariable !! 我又编辑了一次我的问题。请帮我传递变量,谢谢

以上是关于Android 错误:android.widget.TextView.<init>的主要内容,如果未能解决你的问题,请参考以下文章

Android 错误:android.widget.TextView.<init>

安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditT

android.view.inflateexception 二进制 xml 文件第 1 行错误膨胀类 android.widget.relativeLayout

setSupportActionBar 工具栏无法应用到 (android.widget.Toolbar) 错误

Android.widget,textView 不能转换为 android.widget,button

如何修复 Android Widget 的“进程错误”错误?