此代码停止工作,我不明白为什么。 当它尝试执行StartActivity(Intent)[在代码中标记,在OnClick内部时]它返回此并崩溃:
“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.view.View.setOnClickListener(android.view.View $ OnClickListener)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)“
这是活动:
public class MainActivity extends ListActivity implements View.OnClickListener {
TextView responseField;
private ScoresDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseField = (TextView)findViewById(R.id.textView);
findViewById(R.id.buttonScore).setOnClickListener(this);
findViewById(R.id.buttonAdd).setOnClickListener(this);
datasource = new ScoresDataSource(this);
datasource.open();
List<Score> scores = datasource.getAllScores();
ArrayAdapter<Score>
adapter = new ArrayAdapter<Score>(
this,
android.R.layout.simple_list_item_1,
scores);
setListAdapter(adapter); }
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view)
{
ArrayAdapter<Score> adapter =
(ArrayAdapter)getListAdapter();
Score c = null;
switch (view.getId()){
case R.id.buttonScore:
Intent scorePage = new Intent(this, ScoreboardActivity.class);
startActivity(scorePage);
//[CRASHES HERE] <---------------
break;
case R.id.buttonAdd:
EditText nameField = (EditText)findViewById(R.id.name);
EditText scoreField = (EditText)findViewById(R.id.score);
String name = nameField.getText().toString();
String score = scoreField.getText().toString();
try {
int scoreInt = Integer.parseInt(score);
responseField.setText("SCORE RECEIVED: " + System.getProperty("line.separator") + name + " " + score);
c = datasource.createScore(score, name);
adapter.add(c);
}
catch(Exception e){
responseField.setText("Not Valid Score");
}
adapter.notifyDataSetChanged();
break; }
}
}
这是活动的xml表:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="271dp"
android:layout_height="wrap_content"
android:text="AddScore"
android:id="@+id/buttonAdd" />
<EditText
android:layout_width="187dp"
android:text="Player"
android:layout_height="wrap_content"
android:id="@+id/name" />
<EditText
android:layout_width="184dp"
android:text="Score"
android:layout_height="wrap_content"
android:id="@+id/score" />
<TextView
android:layout_width="226dp"
android:layout_height="70dp"
android:text=""
android:id="@+id/textView" />
<Button
android:layout_width="222dp"
android:layout_height="73dp"
android:text="ScoreBoard"
android:id="@+id/buttonScore" />
<ListView
android:layout_width="39dp"
android:layout_height="24dp"
android:id="@android:id/list"
android:layout_gravity="center_horizontal"
android:visibility="invisible" />
Android Manifest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".ScoreboardActivity"
android:label="Scoreboard" >
</activity>
</application>
目标活动:
public class ScoreboardActivity extends ListActivity implements View.OnClickListener {
private ScoresDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scores);
findViewById(R.id.buttonAdd).setOnClickListener(this);
findViewById(R.id.buttonDelete).setOnClickListener(this);
datasource = new ScoresDataSource(this);
datasource.open();
List<Score> scores = datasource.getAllScores();
ArrayAdapter<Score>
adapter = new ArrayAdapter<Score>(
this,
android.R.layout.simple_list_item_1,
scores);
//Sorting
adapter.sort(new Comparator<Score>() {
@Override
public int compare(Score sc1, Score sc2) {
return Integer.toString(sc1.getScore()).compareTo(Integer.toString(sc2.getScore()));
}
});
setListAdapter(adapter);
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view)
{
ArrayAdapter<Score> adapter =
(ArrayAdapter)getListAdapter();
Score c = null;
Random r = new Random();
switch (view.getId()){
case R.id.buttonBack:
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
break;
case R.id.buttonDelete:
if(adapter.getCount()>0){
c = adapter.getItem(0);
datasource.deleteScore(c);
adapter.remove(c);
}
break;
}
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:1)
您的错误告诉您,您正尝试在空对象上设置点击侦听器。
如果您说启动下一个活动时会触发错误,那么在ScoreboardActivity
上,尝试为其中一个视图设置点击监听器时会出错:
findViewById(R.id.buttonAdd).setOnClickListener(this);
findViewById(R.id.buttonDelete).setOnClickListener(this);
检查您的activity_scores.xml
,看看您是否确实拥有这些ID。