【问题标题】:setText make apk crashsetText 使 apk 崩溃
【发布时间】:2012-04-24 17:13:15
【问题描述】:

我正在尝试开发一个非常简单的 apk。
我正在使用一个 textView 来显示我在之前的活动中输入的两个团队的名称(这里是为了打开这个活动而带来的)。 当我尝试使用 setText 显示这些团队的名称时,apk 崩溃了。

这是崩溃的类:

    public class MatchPage extends Activity {
private String locali= null;
private String ospiti= null;
private TextView localiTV;
private TextView ospitiTV;
private MatchRugby partita;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.match);
     localiTV =(TextView) findViewById(R.id.localiTV);
     ospitiTV =(TextView) findViewById(R.id.ospitiTV);
    getLocali();
    getOspiti();
    createMatch();



     localiTV.setText("Locali /n"+ partita.teamA.getName());
     ospitiTV.setText("Ospiti /n"+ partita.teamB.getName());




}

/**
 * Prende il nome della squadra locale dall'intent
 * @return
 */
public String getLocali(){
    Intent matchStart = getIntent();
    String locali = matchStart.getStringExtra(NewMatchPage.LOCALI);
    return locali;
}

/**
 * prende il nome della squadra ospite dall'intent
 * @return
 */
public String getOspiti(){
    Intent matchStart = getIntent();
    String ospiti = matchStart.getStringExtra(NewMatchPage.OSPITI);
    return ospiti;
}

public MatchRugby createMatch(){
    TeamRugby teamLocali= new TeamRugby(locali);
    TeamRugby teamOspiti= new TeamRugby(ospiti);
    MatchRugby partita= new MatchRugby(teamLocali, teamOspiti);
    return partita;
}
    }

这是 XML:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/localiTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textAppearance="?android:attr/textAppearanceLarge" />


<TextView
    android:id="@+id/ospitiTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

这是发送意图的类:

   public class NewMatchPage extends Activity {

public static final String LOCALI = null;
public static final String OSPITI = null;
   private Button startMatch;
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_match);

     startMatch= (Button) findViewById(R.id.startMatch);
        startMatch.setOnClickListener(new View.OnClickListener(){


            public void onClick(View arg0) {
                startMatch();

            }

        });
}

public void startMatch(){
    Intent startMatch= new Intent(this, MatchPage.class);

    //Prendo il testo scritto nella casella locali e la porto nella partita
    EditText locali= (EditText) findViewById(R.id.Locali);
    String locali1 = locali.getText().toString();
    startMatch.putExtra(LOCALI, locali1);
    //Prendo il testo scritto nella casella ospiti e la porto nella partita
    EditText ospiti= (EditText) findViewById(R.id.Ospiti);
    String ospiti1 = ospiti.getText().toString();
    startMatch.putExtra(OSPITI, ospiti1);
    //inizio la partita
    startActivity(startMatch);
}

    }

最后是 logcat 日志:

    04-24 16:49:08.928: W/dalvikvm(1377): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
    04-24 16:49:08.958: E/AndroidRuntime(1377): FATAL EXCEPTION: main
    04-24 16:49:08.958: E/AndroidRuntime(1377): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.gmail.david.corsalini.sportscout/com.gmail.david.corsalini.sportscout.MatchPage}: java.lang.NullPointerException
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.os.Looper.loop(Looper.java:137)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at dalvik.system.NativeStart.main(Native Method)
    04-24 16:49:08.958: E/AndroidRuntime(1377): Caused by: java.lang.NullPointerException
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at com.gmail.david.corsalini.sportscout.MatchPage.onCreate(MatchPage.java:25)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.Activity.performCreate(Activity.java:4465)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    04-24 16:49:08.958: E/AndroidRuntime(1377):     ... 11 more

MatchRugby 类

    public class MatchRugby {
public TeamRugby teamA;
public TeamRugby teamB;

/**
 * Costruttore della partita
 */
public MatchRugby(TeamRugby teamA, TeamRugby teamB){
    this.teamA=teamA;
    this.teamB=teamB;
}

/**
 * @return the teamA
 */
public TeamRugby getTeamA() {
    return teamA;
}


/**
 * @param teamA the teamA to set
 */
public void setTeamA(TeamRugby teamA) {
    this.teamA = teamA;
}


/**
 * @return the teamB
 */
public TeamRugby getTeamB() {
    return teamB;
}


/**
 * @param teamB the teamB to set
 */
public void setTeamB(TeamRugby teamB) {
    this.teamB = teamB;
}


public void EndOfMatch(){
    //nothing to do with the problem
    }
}

【问题讨论】:

    标签: java android android-layout settext


    【解决方案1】:

    您的变量范围已全部关闭。像这样设置你的课程:

    public class MatchPage extends Activity {
    private String locali= null;
    private String ospiti= null;
    private TextView localiTV;
    private TextView ospitiTV;
    private MatchRugby partita;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.match);
         localiTV =(TextView) findViewById(R.id.localiTV);
         ospitiTV =(TextView) findViewById(R.id.ospitiTV);
        getLocali();
        getOspiti();
        createMatch();
    
         localiTV.setText("Locali /n"+ partita.teamA.getName());
         ospitiTV.setText("Ospiti /n"+ partita.teamB.getName());
    
    }
    
    /**
     * Prende il nome della squadra locale dall'intent
     * @return
     */
    public String getLocali(){
        if (locali == null) {
          Intent matchStart = getIntent();
          locali = matchStart.getStringExtra(NewMatchPage.LOCALI);
        }
        return locali;
    }
    
    /**
     * prende il nome della squadra ospite dall'intent
     * @return
     */
    public String getOspiti(){
        if (ospiti == null) {
          Intent matchStart = getIntent();
          ospiti = matchStart.getStringExtra(NewMatchPage.OSPITI);
        }
        return ospiti;
    }
    
    public MatchRugby createMatch(){
        TeamRugby teamLocali= new TeamRugby(locali);
        TeamRugby teamOspiti= new TeamRugby(ospiti);
        partita = new MatchRugby(teamLocali, teamOspiti);
        return partita
    }
    
      private String locali
      private String ospiti
      private MatchRugby partita
    }
    

    【讨论】:

    • 我没有看到你在哪里更改了代码,你只是在最后添加了 locali ospiti 和 partita 的副本,或者我什么都没找到?
    • @DavidCorsalini 抱歉,我没有在顶部看到您的声明。最后你可以忽略它们。主要变化在于您的函数从:MatchRugby partita = new MatchRugby(teamLocali, teamOspiti); 变为 partita = new MatchRugby(teamLocali, teamOspiti);。通过再次添加类型,您创建了一个仅存在于函数内部的局部变量,然后被销毁。删除指向类级别变量的类型。对 locali 和 ospiti 执行相同操作
    • 这成功了,谢谢,现在我只需要找到我搞砸的地方并在 localiTV 上使用 setText 的 ospiti 值!
    • @DavidCorsalini 一定要接受和/或支持解决/帮助您的问题的答案 - 欢迎来到 StackOverflow :)
    【解决方案2】:

    您在onCreate 方法中MatchPage 类的第25 行有一个NullPointerException。不确定您发布的代码中到底是哪一行,但我最好的猜测是您的 findViewById 调用找不到任何内容。

    【讨论】:

      【解决方案3】:
      java.lang.NullPointerException
         at com.gmail.david.corsalini.sportscout.MatchPage.onCreate(MatchPage.java:25)
      

      似乎您的 partita 对象因上述错误为空。从你初始化它的代码中

      createMatch();
      

      用下面的代码替换上面的方法

      partita = createMatch();
      

      您也可以更改以下代码::

      public void createMatch(){
          TeamRugby teamLocali= new TeamRugby(locali);
          TeamRugby teamOspiti= new TeamRugby(ospiti);
          partita= new MatchRugby(teamLocali, teamOspiti);
      } 
      

      【讨论】:

      • 通过这样做我得到一个错误,因为 createMatch 的返回类型不是 MatchRugby (但 void)。只需将方法调用替换为 partita = createMatch();我有一个工作活动,但 textview 给了我“locali null”和“ospiti null”。
      • 你能不能添加你的 MatchRugby 类可能需要改一下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多