2014-02-25 181 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(); 
相关问题