3

我需要将子项动态添加到可扩展列表视图中。在我在 addshare 活动中添加我的记录后,我转到另一个传递字符串对象的活动(动态 1)。在这里,我使用 String 对象作为参数添加新记录,并将意图传递回 MainActivity。Investment_summary_main.xml 包含可展开的列表视图。

动态 1 的代码:

public class dynamic1 extends Activity{
MyCustomAdapter c=new MyCustomAdapter();
String s;
 private ExpandableListView mExpandableList;
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.investment_summary_main);
      if(getIntent().getExtras()!=null)
      { Intent intent = getIntent();
       s = intent.getExtras().getString("k");
      }
      mExpandableList= (ExpandableListView) this.findViewById(R.id.expandable_list);
      ExpandableListAdapter adapter =  (ExpandableListAdapter) mExpandableList.getExpandableListAdapter();
((parent)adapter.getGroup(0)).getArrayChildren().add(s);
((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}

添加共享代码:

public class addshare extends Activity{
DBAdapter db = new DBAdapter(this);
MyCustomAdapter c=new MyCustomAdapter();
 private ExpandableListView mExpandableList;
  public void addshare(){}

@Override
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.add);
}

    public void addingshare(View v)
    {
        Log.d("test", "adding");
        //get data from form
        //EditText intypeid = (EditText)findViewById(R.id.intypeid);
        EditText sharename= (EditText)findViewById(R.id.sharename);
        EditText shareid = (EditText)findViewById(R.id.shareid);
        EditText purpri = (EditText)findViewById(R.id.purpri);
        EditText sharepri = (EditText)findViewById(R.id.sharepri);
        EditText shareno = (EditText)findViewById(R.id.shareno);
        EditText purdate = (EditText)findViewById(R.id.purdate);
        EditText purplace = (EditText)findViewById(R.id.purplace);
        EditText purcon = (EditText)findViewById(R.id.purcon);
        Float totalinvestment=0F,currmarketvalue=0F;


           String sname=sharename.getText().toString();

         /*  Intent i = new Intent(this, dynamicchild.class);
           i.putExtra("k", sname);
           startActivity(i); 
          */

           String s1=sharepri.getText().toString();
           Float n1=Float.parseFloat(s1);

           String s2=purpri.getText().toString();
           Float n2=Float.parseFloat(s2);

         //  shares_equities s=new shares_equities( sname);
           String s3=shareno.getText().toString();
           Integer i1=Integer.parseInt(s3);
           db.open();        
           long id = db.addRecord(sname, shareid.getText().toString(),
                 n2,n1,i1, purdate.getText().toString(),purplace.getText().toString(),purcon.getText().toString()
                 ,totalinvestment,currmarketvalue);  

           db.close();

        sharename.setText("");
        shareid.setText("");
        purpri.setText("");
        sharepri.setText("");
        shareno.setText("");
        purdate.setText("");
        purplace.setText("");
        purcon.setText("");


        Toast.makeText(addshare.this,"share record Added", Toast.LENGTH_LONG).show();  


        Intent i = new Intent(this, dynamic1.class);
           i.putExtra("k", sname);
           startActivity(i);

    }
     public void viewAssignments(View v)
    {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
    }

 }

Investment_summary_main.xml 的代码

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/slist"
           >

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical" >


       <ExpandableListView
           android:id="@+id/expandable_list"
           android:layout_width="fill_parent"
           android:layout_height="400dp"
           android:layout_weight="2900208.75"
           android:cacheColorHint="#00000000"
           android:listSelector="@android:color/transparent"
           android:transcriptMode="disabled" />

       <Button
           android:id="@+id/addnew"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:layout_margin="5dp"
           android:text="ADD NEW"
           android:onClick="sendMessage_addrecord" />
   </LinearLayout>

最后我在添加记录时得到的错误:

06-23 01:40:40.997: E/AndroidRuntime(484): FATAL EXCEPTION: main
06-23 01:40:40.997: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.example.moolah/com.example.moolah.dynamic1}: java.lang.NullPointerException
06-23 01:40:40.997: E/AndroidRuntime(484):  at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-23 01:40:40.997: E/AndroidRuntime(484):  at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.os.Looper.loop(Looper.java:137)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.main(ActivityThread.java:4340)
06-23 01:40:40.997: E/AndroidRuntime(484):  at java.lang.reflect.Method.invokeNative(Native Method)
06-23 01:40:40.997: E/AndroidRuntime(484):  at java.lang.reflect.Method.invoke(Method.java:511)
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-23 01:40:40.997: E/AndroidRuntime(484):  at dalvik.system.NativeStart.main(Native Method)
06-23 01:40:40.997: E/AndroidRuntime(484): Caused by: java.lang.NullPointerException
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.example.moolah.dynamic1.onCreate(dynamic1.java:31)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.Activity.performCreate(Activity.java:4465)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-23 01:40:40.997: E/AndroidRuntime(484):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
06-23 01:40:40.997: E/AndroidRuntime(484):  ... 11 more
4

2 回答 2

3

好吧,在检查了你的整个代码之后,我发现你用这种方式初始化了你的自定义适配器:

// notice that it's an empty constructor
MyCustomAdapter c = new MyCustomAdapter();

看着你的MyCustomAdapter.java我发现这个类有两个构造函数。一个是空的构造函数,另一个是这样的:

// You should use this one instead
public MyCustomAdapter(Context context, ArrayList<parent> parent){
         mParent = parent;
         c = context;
         inflater = LayoutInflater.from(context);
}

在您的活动中,您使用不接收and的空构造函数dynamic1初始化,因此当您调用时:cparentcontext

int  i = c.getChildrenCount(0);

你会得到 NPE,因为parentis null。要对其进行排序,请c使用正确的构造函数实例化:

MyCustomAdapter c = new MyCustomAdapter(YourActivityContext, ParentArrayList);
于 2013-06-23T17:54:24.630 回答
1

您似乎从未分配过适配器,因此列表中没有可返回给您的适配器。查看本教程,了解如何实现SimpleExpandableListAdapter

于 2013-06-22T22:45:39.063 回答