这将是一个冗长的问题,其中包含大量代码,因此对于那些花时间阅读它的人来说非常感谢!
所以基本上我有一个使用 Eclipse 制作的应用程序。MAIN ACTIVITY 扩展为 FragmentActivity,称为 MenuMain.java。此活动在其 xml 中定义了三个片段。我遇到的问题是第三个。
我想要做的是当用户单击中间片段上的按钮时,它会将内容添加到作为第三个片段的自定义列表视图中。
这是我在中间片段上显示内容的课程。
ImageFragment.java
package com.thesis.menubook;
import java.util.ArrayList;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ImageFragment extends Fragment {
ProgressDialog pDialog;
private int imageResourceId;
private ArrayList<String> menu_id = new ArrayList<String>();
private ArrayList<String> menu_name = new ArrayList<String>();
private ArrayList<String> menu_description = new ArrayList<String>();
int page = 0;
String orderQuantity = "";
EditText quantity;
ListView lv;
public ImageFragment(int imageResource, ArrayList<String> mid, ArrayList<String> mname, ArrayList<String> mdesc, int pg) {
this.imageResourceId = imageResource;
this.menu_id = mid;
this.menu_name = mname;
this.menu_description = mdesc;
this.page = pg;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_menu, container, false);
Button next = (Button)view.findViewById(R.id.next);
Button back = (Button)view.findViewById(R.id.back);
Button add = (Button)view.findViewById(R.id.add);
quantity = (EditText) view.findViewById(R.id.editText1);
RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.main_layout);
layout.setBackgroundResource(imageResourceId);
//Change ID on Layout
TextView id = (TextView) view.findViewById(R.id.menu_id);
id.setText(menu_id.get(page));
//Change name on layout
TextView name = (TextView) view.findViewById(R.id.menu_name);
name.setText(menu_name.get(page));
//change description on layout
TextView desc = (TextView) view.findViewById(R.id.menu_description);
desc.setText(menu_description.get(page));
// set next page button listener
next.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(page < menu_id.size()-1)
{
page++;
Toast.makeText(getActivity(), "Next", Toast.LENGTH_LONG).show();
loadMenuItem(page);
}
else
{
Toast.makeText(getActivity(), "You are at the end of the page", Toast.LENGTH_LONG).show();
}
}
});
// set back page button listener
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(page != 0)
{
page--;
Toast.makeText(getActivity(), "Next", Toast.LENGTH_LONG).show();
loadMenuItem(page);
}
else
{
Toast.makeText(getActivity(), "You are at the start of the page", Toast.LENGTH_LONG).show();
}
}
});
//set add to orderlist button listener
add.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if(quantity.getText().toString() != "" || quantity.getText().toString() != "0" || quantity.getText().toString() != "00"){
lv = (ListView) v.findViewById(R.id.orderListView);
new AddToList().execute("");
}
else
{
Toast.makeText(getActivity(), "Please put quantity", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
public void loadMenuItem(int pahina) {
FragmentManager fm = getFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.menu_fragment, new ImageFragment(R.drawable.caesar_salad, menu_id, menu_name,menu_description, pahina));
ft.commit();
}
}
class AddToList extends AsyncTask<String, String, String> {
ArrayList<DataArray> dataArray = new ArrayList<DataArray>();
DataArray data = new DataArray();
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
Log.d("Activity Context", getActivity().toString() + "");
pDialog.setMessage("Adding to list. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... param) {
data.setID(menu_id.get(page));
data.setName(menu_name.get(page));
data.setQuantity(quantity.getText().toString());
dataArray.add(data);
Log.d("Order Data ID: ", data.getID() + "");
Log.d("Order Data Name: ", data.getName() + "");
Log.d("Order Data Quantity: ", data.getQuantity() + "");
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
lv.setAdapter(new MyCustomAdapter(getActivity(), dataArray));
}
}
}
lv.setAdapter(new MyCustomAdapter(getActivity(), dataArray));
当我在 onPostExecute() 上的这一行中调用我的自定义适配器时,这里出现错误
我这样定义我的自定义适配器
MyCustomAdapter.java
package com.thesis.menubook;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyCustomAdapter extends BaseAdapter {
private static ArrayList<DataArray> orderData;
private LayoutInflater mInflater;
public MyCustomAdapter(Context context, ArrayList<DataArray> passed) {
orderData = passed;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return orderData.size();
}
public Object getItem(int position) {
return orderData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_order_list, null);
holder = new ViewHolder();
holder.txtID = (TextView) convertView.findViewById(R.id.id);
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtQuantity = (TextView) convertView.findViewById(R.id.quantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtID.setText(orderData.get(position).getID());
holder.txtName.setText(orderData.get(position).getName());
holder.txtQuantity.setText(orderData.get(position).getQuantity());
return convertView;
}
static class ViewHolder {
TextView txtID;
TextView txtName;
TextView txtQuantity;
}
}
而我的自定义变量 DataArray.java 是这样的。我对 DataArray 没有任何问题,因为它记录了内容,所以我确信 NPE 错误与我对此类的使用无关
包 com.thesis.menubook;
公共类数据数组 {
private String menu_ID = "";
private String menu_name = "";
private String quantity = "";
public void setID(String ID) {
this.menu_ID = ID;
}
public String getID() {
return menu_ID;
}
public void setName(String menu_name) {
this.menu_name = menu_name;
}
public String getName() {
return menu_name;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getQuantity(){
return quantity;
}
}
我认为问题是因为 ImageFragment 上的getActivity()
这一行lv.setAdapter(new MyCustomAdapter(getActivity(), dataArray));
。我试过了:
getActivity().getApplicationCOntext()
getActivity().getBaseContext()
getActivity().getParent().getApplicationContext()
getActivity().getParent().getBaseContext()
无济于事。
那么我该如何解决呢?
以下是您可能需要分析以帮助我的其他不太重要的类和 xml 文件。
OrderListFragment.java
package com.thesis.menubook;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class OrderListFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_order_list, container, false);
return view;
}
}
activity_order_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/orderlist"
android:layout_width="505dp"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:text="@string/orderlist"
android:textSize="17sp"
android:textStyle="bold" />
<ListView
android:id="@+id/orderListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
custom_order_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
activity_menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/activity_main_large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" xmlns:tools="http://schemas.android.com/tools">
<fragment
android:id="@+id/menu_category_fragment"
android:name="com.thesis.menubook.MenuCategory"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.14"
layout="@layout/activity_menu_category" />
<fragment
android:id="@+id/menu_fragment"
android:name="com.thesis.menubook.MenuFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.71"
layout="@layout/activity_menu_fragment" />
<fragment
android:id="@+id/orderlist_fragment"
android:name="com.thesis.menubook.OrderListFragment"
android:layout_width="167dp"
android:layout_height="match_parent"
layout="@layout/activity_order_list" />
</LinearLayout>
layout_menu.xml(中间片段内容xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/caesar_salad"
android:gravity="top|bottom|center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:visibility="visible" >
<TextView
android:id="@+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_name"
android:textSize="40sp"
android:background="#2f000000" android:textColor = "@android:color/white"/>
<TextView
android:id="@+id/menu_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_name"
android:textSize="40sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/menu_name"
android:background="#2f000000" android:textColor = "@android:color/white"
android:visibility="invisible"/>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/menu_name"
android:layout_marginTop="20dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/menu_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2f000000"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="italic"
android:typeface="sans"
android:text ="@string/menu_description"
android:layout_margin="10dp">
</TextView>
<TextView
android:id="@+id/menu_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2f000000"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="italic"
android:typeface="sans"
android:text ="@string/menu_price"
android:layout_margin="10dp"/>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/menu_description"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:background="#2f000000"
android:text="@string/quantity"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="italic"
android:typeface="sans"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/quantity"
android:layout_alignTop="@+id/quantity"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/quantity"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:ems="2"
android:maxLength="2"
android:inputType="number">
<requestFocus />
</EditText>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_toRightOf="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="@string/add"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tableLayout1"
android:layout_alignRight="@+id/tableLayout1"
android:layout_marginRight="42dp"
android:text="@string/next"
android:textSize="20sp" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tableLayout1"
android:layout_marginRight="26dp"
android:layout_toLeftOf="@+id/next"
android:text="@string/back"
android:textSize="20sp" />
</RelativeLayout>
这是我的 LogCat
02-16 13:25:25.541: D/Order Data ID:(502): 001
02-16 13:25:25.541: D/Order Data Name:(502): Chicken
02-16 13:25:25.541: D/Order Data Quantity:(502): 8
02-16 13:25:25.865: E/AndroidRuntime(502): FATAL EXCEPTION: main
02-16 13:25:25.865: E/AndroidRuntime(502): java.lang.NullPointerException
02-16 13:25:25.865: E/AndroidRuntime(502): at com.thesis.menubook.ImageFragment$AddToList.onPostExecute(ImageFragment.java:182)
02-16 13:25:25.865: E/AndroidRuntime(502): at com.thesis.menubook.ImageFragment$AddToList.onPostExecute(ImageFragment.java:1)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.os.AsyncTask.finish(AsyncTask.java:590)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.os.AsyncTask.access$600(AsyncTask.java:149)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.os.Looper.loop(Looper.java:132)
02-16 13:25:25.865: E/AndroidRuntime(502): at android.app.ActivityThread.main(ActivityThread.java:4025)
02-16 13:25:25.865: E/AndroidRuntime(502): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 13:25:25.865: E/AndroidRuntime(502): at java.lang.reflect.Method.invoke(Method.java:491)
02-16 13:25:25.865: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-16 13:25:25.865: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-16 13:25:25.865: E/AndroidRuntime(502): at dalvik.system.NativeStart.main(Native Method)
02-16 13:25:26.587: D/dalvikvm(502): GC_CONCURRENT freed 126K, 7% free 7635K/8199K, paused 23ms+35ms
02-16 13:25:32.642: I/Process(502): Sending signal. PID: 502 SIG: 9