2012-04-24 77 views
-1

我正在嘗試開發一個非常簡單的apk。
我使用textView來顯示兩個團隊的名稱,我在之前的活動中輸入(帶有打開此活動的意圖)。 當我嘗試使用setText來顯示這些團隊的名稱apk崩潰。setText make 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 
    } 
} 

回答

0

您的變量範圍是全部關閉。設置你的班級是這樣的:

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 
} 
+0

我沒有看到你在哪裏改變了代碼,你只是在最後添加了locali ospiti和partita的副本,或者我沒有找到什麼東西? – 2012-04-24 18:01:47

+0

@DavidCorsalini對不起,我沒有看到你的聲明在頂部。你可以在最後忽略它們。主要變化在於:MatchRugby partita = new MatchRugby(teamLocali,teamOspiti);''partita = new MatchRugby(teamLocali,teamOspiti);'。通過再次添加類型,你創建了一個局部變量,它只存在於你的函數中,然後被銷燬。將類型點移除到類級變量。爲locali和ospiti做同樣的工作 – JRaymond 2012-04-24 18:06:03

+0

這樣做的伎倆,謝謝,現在我只需要找到我在哪裏搞砸了,並在localiTV上使用setText的ospiti值! – 2012-04-24 19:14:27

0
java.lang.NullPointerException 
    at com.gmail.david.corsalini.sportscout.MatchPage.onCreate(MatchPage.java:25) 

似乎是你變奏曲對象是從上述錯誤空。從你的代碼中要初始化它

createMatch(); 

下面一行替換上述方法

partita = createMatch(); 

您也可以更改下面的代碼::

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

通過這樣做,我得到一個錯誤,因爲createMatch的返回類型不是MatchRugby(但是無效)。 只需用partita = createMatch()替換方法調用;我得到一個工作活動,但textview給我「locali null」和「ospiti null」。 – 2012-04-24 18:03:40

+0

您可以添加您的MatchRugby類,可能需要更改它在主要問題上添加 – 2012-04-24 18:12:31

+0

。 – 2012-04-24 19:14:36

0

你有NullPointerExceptiononCreate方法的MatchPage類的#25行。不知道你發佈的代碼究竟是哪一行,但我最好的猜測是你的findViewById調用找不到任何東西。