无法启动活动ComponentInfo {... add_team}:java.lang.NullPointerException

时间:2016-04-09 04:42:02

标签: android android-studio nullpointerexception android-spinner android-logcat

我是Android编程的新手,希望有人可以提供帮助。我正在尝试从数据库表league_name填充列名为Leagues的微调器,并且尝试执行此操作时遇到以下错误:

Process: com.example.android.soccerleaguesapp, PID: 11770
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.soccerleaguesapp/com.example.android.soccerleaguesapp.add_team}: java.lang.NullPointerException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
       at android.app.ActivityThread.access$800(ActivityThread.java:135)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5021)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
       at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
       at com.example.android.soccerleaguesapp.add_team.onCreate(add_team.java:55)
       at android.app.Activity.performCreate(Activity.java:5231)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
       at android.app.ActivityThread.access$800(ActivityThread.java:135) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:136) 
       at android.app.ActivityThread.main(ActivityThread.java:5021) 
       at java.lang.reflect.Method.invokeNative(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:515) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
       at dalvik.system.NativeStart.main(Native Method) 

add_team class

public class add_team extends AppCompatActivity {

 //Calls DatabaseHelper Class
 DatabaseHelper myDb;

 //Create EditText Variable to cast in OnCreate method
 EditText team_name;

 //Create Button Variables to cast in OnCreate method
 Button add_team, get_teams;

 //Create Spinner Variables to cast in OnCreate method
 Spinner league;

 ArrayList < String > leagues = new ArrayList < String > ();

 ArrayAdapter < String > adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.add_team);

  myDb = new DatabaseHelper(this);

  team_name = (EditText) findViewById(R.id.edit_team_name);

  add_team = (Button) findViewById(R.id.insert_team);
  get_teams = (Button) findViewById(R.id.view_teams);

  league = (Spinner) findViewById(R.id.edit_league_id);

  adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, leagues);

  add_team.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {

    leagues.clear();

    Cursor cursor = myDb.getAllLeagues();

    while (cursor.moveToNext()) {
     //Extract the data
     String leagueName = cursor.getString(1);

     leagues.add(leagueName);
    }
    league.setAdapter(adapter);

   }
  });

 }

}

getAllLeagues方法

public Cursor getAllLeagues() {
 //Create SQLiteDatabase instance
 SQLiteDatabase db = this.getReadableDatabase();

 String[] columns = {
  ID,
  LEAGUE_NAME
 };

 //Return query to return cursor object
 return db.query(LEAGUES_TABLE, columns, null, null, null, null, null);
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用On按钮替换您的代码单击

                if (leagues == null) {
                    leagues = new ArrayList<String>();
                }
                leagues.clear();
                Cursor cursor = myDb.getAllLeagues();
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        //Extract the data
                        String leagueName = cursor.getString(1);

                        leagues.add(leagueName);
                    }
                    league.setAdapter(adapter);
                }