2014-02-25 178 views
0

好的,所以我遇到的問題發生在startGame按鈕被按下時。按下按鈕時,應用程序崩潰。該活動在清單中實例化,因此我不確定錯誤在哪裏。意圖的代碼是另一個(工作)的副本,所以我不知道我出錯的地方。無法實例化活動ComponentInfo ... java.lang.NullPointerException

錯誤日誌:

02-25 14:46:51.064:E/AndroidRuntime(1261):致命異常:主 02-25 14:46:51.064:E/AndroidRuntime(1261):進程:com.example.hegemony,PID:1261 02-25 14:46:51.064:E/AndroidRuntime(1261):java.lang.RuntimeException:無法實例化活動ComponentInfo {com.example.hegemony/com.example。 hegemony.PlayerTurn}:顯示java.lang.NullPointerException

清單:

發送活動的
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.hegemony" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="19" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 



    <activity 
     android:name="com.example.hegemony.SplashScreen" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.example.hegemony.StartScreen" > 
     <intent-filter> 
      <action android:name="com.example.hegemony.STARTSCREEN" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 


    <activity 
     android:name="com.example.hegemony.SetupHomeScreen" > 
     <intent-filter> 
      <action android:name="com.example.hegemony.SETUPHOMESCREEN" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.example.hegemony.SetupPlayer" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.example.hegemony.SETUPPLAYER" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.example.hegemony.PlayerTurn" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.example.hegemony.PLAYERTURN" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

</application> 

Java代碼:接收活動

public class SetupHomeScreen extends Activity{ 

private ArrayList<Player> p = GameMaster.players; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_setup_home_screen); 
    getActionBar().hide(); 
    updatePlayers(); 
    Button gotoInput = (Button) findViewById(R.id.btnSetupPlayer); 
    gotoInput.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent toInput = new Intent("com.example.hegemony.SETUPPLAYER"); 
      startActivity(toInput); 
     } 
    }); 

    Button startGame = (Button) findViewById(R.id.btnStartGame); 
    startGame.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent toStart = new Intent("com.example.hegemony.PLAYERTURN"); 
      startActivity(toStart); 
     } 
    }); 

} 


public void updatePlayers(){ 
     TextView playerList = (TextView) findViewById(R.id.playerList); 
     String msg = ""; 
     for(int i=0;i < p.size();i++) 
      msg = msg + "\n - "+p.get(i).getName(); 
     playerList.setText(msg); 
     if(p.size() >=2){ 
      Button enable = (Button) findViewById(R.id.btnStartGame); 
      enable.setEnabled(true); 
     } 
    } 
} 

Java代碼:

public class PlayerTurn extends Activity { 

final ActionBar actionBar = getActionBar(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_player_turn); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    ActionBar.TabListener tabListener = new ActionBar.TabListener() { 

     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 

     } 

     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 

     } 

     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 

     } 

    }; 
} 

} 

回答

0
Button startGame = (Button) findViewById(R.id.btnStartGame); 
    startGame.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent toStart = new Intent(SendingActivity.this, NewActivity.class); 
      startActivity(toStart); 
     } 
    }); 

我覺得這是你應該如何開始的意圖。也許它錯過了一個上下文或「活動名稱」是不正確的做法。我試圖按照你嘗試的方式開始一個活動,它給了我一個錯誤,不是同一個錯誤,但它不起作用。 如果我試圖提供的解決方案不起作用,我很抱歉。這真的是我第一次嘗試幫助某人。我希望它能起作用。

+0

我已經試過了,它不起作用。你遇到了什麼錯誤? – ahno1

+0

未發現活動異常。好吧,讓我試着挖掘更多。如果按照我的建議嘗試,會給你什麼樣的錯誤? –

0

根據logcat錯誤消息,您的活動類無法實例化。實例化涉及任何類成員變量的分配和分配。在PlayerTurn的情況下,只有一個是這樣的:

final ActionBar actionBar = getActionBar(); 

因爲活動的窗口尚未建成getActionBar()的調用拋出一個NullPointerException,但 - 你應該調用的setContentView後getActionBar()()已在onCreate()中調用。只需將該行代碼移動到onCreate()中即可解決此問題。

如果仍想保留這是一個類的成員變量,聲明它但不分配它:

ActionBar actionBar; 

...然後做的onCreate()分配:

actionBar = getActionBar(); 
相關問題