无法启动活动 ComponentInfo...add_team:java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】无法启动活动 ComponentInfo...add_team:java.lang.NullPointerException [重复]【英文标题】:Unable to start activity ComponentInfo...add_team: java.lang.NullPointerException [duplicate]无法启动活动 ComponentInfo...add_team:java.lang.NullPointerException [重复] 【发布时间】:2016-04-09 04:42:02 【问题描述】:我是 android 编程的新手,希望有人能提供帮助。我正在尝试使用数据库表 Leagues
中的列名 league_name
填充微调器,并在尝试这样做时遇到以下错误:
Process: com.example.android.soccerleaguesapp, PID: 11770
java.lang.RuntimeException: Unable to start activity ComponentInfocom.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 类
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);
任何帮助将不胜感激。
【问题讨论】:
在此处发布add_team.xml
文件,以便我们提供完美的解决方案。
@Clara 我没想到这一点并意识到我引用了不正确的 id,应该是 insert_teams
而不是 insert_team
,感谢您为我指明了正确的方向。
@mcclosa 现在你解决了吗?
【参考方案1】:
将您的代码替换为以下 On Button Click
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);
【讨论】:
以上是关于无法启动活动 ComponentInfo...add_team:java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章