2013-12-17 169 views
0

我有一個包含培訓列表的Mainactivity。並且在每個列表上單擊它開始整個應用程序中相同訓練的會話。 我設法使用Singleton並將其訪問到我的主要活動中。但onItemClick它需要對話框和按鈕單擊對話框內,它應該採取其他活動。現在我得到一個錯誤,如java NullPointerException。這是下面的代碼; 請記住:我也想在第二項活動中進行同樣的訓練。將同一個對象從一個活動傳遞給另一個活動

MainActivity class;

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

    currentTraining = (Training)arg0.getAdapter().getItem(arg2); 

    Log.i("DEBUG", currentTraining.getTitle()); 

    CurrentTraining.getInstance().setTraining(currentTraining); 

    Toast.makeText(
      getApplicationContext(), 
      "You clicked on position : " + arg2 + " and ID : " 
        + currentTraining.getId(), Toast.LENGTH_LONG).show(); 

    dialog = new Dialog(MainActivity.this); 
    dialog.setContentView(R.layout.dialog); 

    dialog.setTitle(currentTraining.getTitle()); 

    TextView description = (TextView) dialog.findViewById(R.id.textView1); 
    description.setText("Description: " + currentTraining.getDescription()); 

    TextView location = (TextView) dialog.findViewById(R.id.textView2); 
    location.setText("Location: " + currentTraining.getLocation()); 

    TextView date = (TextView) dialog.findViewById(R.id.textView3); 
    date.setText("Date: " + currentTraining.getDate()); 

    Button back_btn = (Button) dialog.findViewById(R.id.button1); 
    back_btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      dialog.dismiss(); 

     } 
    }); 

    Button start_btn = (Button) dialog.findViewById(R.id.button2); 
    start_btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(MainActivity.this, 
        TraineeActivity.class); 

      //intent.putExtra("trainingId", currentTraining.getId()); 

      //intent.putExtra("title", currentTraining.getTitle().toString()); 

      MainActivity.this.startActivity(intent); 
     } 
    }); 

    dialog.show(); 
} 

而在第二個Activity Class中;

Training currentTraining; 

private ListView personNamesListView; 

// Adapter to made the connection between ListView UI component and SQLite data set. 
private ListAdapter traineeListAdapter; 

private TextView TitleView; 

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

    currentTraining = CurrentTraining.getInstance().setTraining(currentTraining); 
    Log.i("DEBUG", ""+currentTraining.getTitle()); 

    TitleView = (TextView)findViewById(R.id.training_title); 
    TitleView.setText(currentTraining.getTitle()); 

    Button addnew = (Button) findViewById(R.id.add_btn); 
    addnew.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(TraineeActivity.this, 
        FormActivity.class); 
      TraineeActivity.this.startActivity(intent); 
     } 
    }); 
    personNamesListView = (ListView) findViewById(R.id.traineeslist); 

    // Create a list that contains only full name of the trainee 
    traineeListAdapter = new ArrayAdapter<Trainee>(this, 
      android.R.layout.simple_list_item_1, currentTraining.getTraineeArrayList()); 
    personNamesListView.setAdapter(traineeListAdapter); 

} 

protected void onResume(){ 
    super.onResume(); 
    traineeListAdapter = new ArrayAdapter<Trainee>(this, android.R.layout.simple_list_item_1, currentTraining.getTraineeArrayList()); 
    personNamesListView.setAdapter(traineeListAdapter); 

} 

我的單例類:

public class CurrentTraining { 

private Training training ; //Training is my model class 

private static CurrentTraining instance; 

private CurrentTraining() { 

} 

public static CurrentTraining getInstance() { 
    if (instance == null) 
     instance = new CurrentTraining(); 
    return instance; 

} 

public Training getTraining() { 
    return training; 
} 

public Training setTraining(Training training) { 
    return this.training = training; 
} 

}

回答

0

在你的第二個活動onCreate(),你這樣做:

currentTraining = CurrentTraining.getInstance().setTraining(currentTraining); 

由於currentTrainingnull,這將只設置單身變量training t null並返回null

你在哪裏設置用戶選擇的值?

相關問題