我试图从一个片段调用我的活动,它在开始时崩溃。
这是调用活动的片段代码(Extends SherlockFragment):
public class SalesOrder extends SherlockFragment{
ListView myInfoList;
String[] items = {"Item 1", "Item 2"};
ArrayAdapter<CharSequence> adapter;
ArrayList<MessageDetails> details;
AdapterView.AdapterContextMenuInfo info;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.sales_order, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
refreshListView();
}
private void refreshListView()
{
myInfoList = (ListView)getActivity().findViewById(R.id.MessageList);
adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
myInfoList.setAdapter(adapter);
details = new ArrayList<MessageDetails>();
MessageDetails Detail;
//CHANGE THIS PART TO RETRIEVE DATABASE ITEM DYNAMICALLY
List<String> itemName = new ArrayList<String>();
itemName.add("Shirt Design 1");
itemName.add("Shirt Design 2");
itemName.add("Shirt Design 3");
List<Float> itemPrice = new ArrayList<Float>();
itemPrice.add(14f);
itemPrice.add(15f);
itemPrice.add(16f);
List<String> description = new ArrayList<String>();
description.add("This is a fine shirt 1");
description.add("This is a fine shirt 2");
description.add("This is a fine shirt 3");
Detail = new MessageDetails();
for (int i =0; i < itemName.size(); i++)
{
Detail = new MessageDetails();
Detail.setSub(itemName.get(i));
Detail.setSubValue(itemPrice.get(i));
Detail.setDescription(description.get(i));
details.add(Detail);
}
myInfoList.setAdapter(new CustomAdapter(details , getActivity().getApplicationContext()));
myInfoList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//System.out.println("Name: "+details.get(position).getSub();
//String s = details.get(position).getDescription();
Intent i = new Intent(getActivity(), purchase.class);
getActivity().startActivity(i);
}
});
}
public class CustomAdapter extends BaseAdapter {
private ArrayList<MessageDetails> _data;
Context _c;
CustomAdapter (ArrayList<MessageDetails> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mysaleslistview, null);
}
TextView subView = (TextView)v.findViewById(R.id.itemName);
TextView subValueView = (TextView)v.findViewById(R.id.itemPrice);
MessageDetails msg = _data.get(position);
subView.setText(msg.sub);
subValueView.setText(msg.subValue.toString());
return v;
}
}
public class MessageDetails {
String sub, description;
Float subValue;
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public Float getSubValue() {
return subValue;
}
public void setSubValue(Float subValue) {
this.subValue = subValue;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}
我的活动叫做(扩展活动):
public class purchase extends Activity {
Button Btnpurchase;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.purchase);
Btnpurchase = (Button) findViewById(R.id.btnPurchase);
}
}
我的logcat:
07-04 23:35:46.318: E/AndroidRuntime(7924): FATAL EXCEPTION: main
07-04 23:35:46.318: E/AndroidRuntime(7924): java.lang.NullPointerException
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
07-04 23:35:46.318: E/AndroidRuntime(7924): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.Activity.performSaveInstanceState(Activity.java:1155)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1216)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3258)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3318)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.ActivityThread.access$900(ActivityThread.java:156)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.os.Looper.loop(Looper.java:137)
07-04 23:35:46.318: E/AndroidRuntime(7924): at android.app.ActivityThread.main(ActivityThread.java:5233)
07-04 23:35:46.318: E/AndroidRuntime(7924): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 23:35:46.318: E/AndroidRuntime(7924): at java.lang.reflect.Method.invoke(Method.java:511)
07-04 23:35:46.318: E/AndroidRuntime(7924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-04 23:35:46.318: E/AndroidRuntime(7924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-04 23:35:46.318: E/AndroidRuntime(7924): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
将您的意图改为:
Intent i = new Intent(getActivity(), purchaseDialog.class);
P.S。
命名活动类purchaseDialog不是很好的做法imho。
答案 1 :(得分:0)
尝试下面的
Intent i = new Intent(getActivity(), purchaseDialog.class);
startActivity(i);
建议试试这个
myInfoList = (ListView)view.findViewById(R.id.MessageList);
答案 2 :(得分:0)
您没有提到调用具有Fragment的Activity的内容。我现在只称它为MyActivity
。
尝试:
Intent i = new Intent(MyActivity.this, purchase.class);
MyActivity.this.startActivity(i);