如何在Activity类中实现Singleton类(How to implement Singleton class in Activity Class)
我想在整个应用程序中提供单一培训。 让我们从主要活动列表视图中说出每个单一的培训。 但我没有从Singleton类中获取值。 我的代码中有什么问题。 我从昨天开始一直在努力,想不通为什么? 感谢您的帮助。
这是Singleton类:
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; }}
我的主要活动课程:
public class MainActivity extends ListActivity implements OnItemClickListener { private Portfolio portfolio; private ListAdapter trainingListAdapter; private Dialog dialog; Training currentTraining; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); currentTraining = CurrentTraining.getInstance().getTraining(); portfolio = new Portfolio(this); trainingListAdapter = new ArrayAdapter<Training>(this, android.R.layout.simple_list_item_1, portfolio.getTrainingArrayList()); setListAdapter(trainingListAdapter); ((ListView) findViewById(android.R.id.list)) .setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 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(); }OnClick到listView它因为NullPointer异常而崩溃。
I want to make my a single Training available in whole application. Let's say each single Training from main activity listview. But I am not getting the values from Singleton class. What is the problem in my code. I have been trying since yesterday and couldn't figure out why?? Thanks for your help in advance.
Here is Singleton class:
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; }}
My Main activity class:
public class MainActivity extends ListActivity implements OnItemClickListener { private Portfolio portfolio; private ListAdapter trainingListAdapter; private Dialog dialog; Training currentTraining; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); currentTraining = CurrentTraining.getInstance().getTraining(); portfolio = new Portfolio(this); trainingListAdapter = new ArrayAdapter<Training>(this, android.R.layout.simple_list_item_1, portfolio.getTrainingArrayList()); setListAdapter(trainingListAdapter); ((ListView) findViewById(android.R.id.list)) .setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 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(); }OnClick to the listView it crashes because of NullPointer Exception.
最满意答案
Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining);没有设定价值,如何获得价值?
@Override 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(); } Log.i ("DEBUG" , currentTraining.getTitle()); CurrentTraining.getInstance().setTraining(currentTraining);not ever set value, how to get the value?
@Override 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(); }更多推荐
发布评论