0

我正在尝试为我的项目中的特定组(例如第一个组)动态添加记录。这是我想出的代码,我在添加记录时插入。这样当我添加一条记录时,就会创建一个新的孩子:

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();

           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);  
          //COMMENTED BELOW AS IT GENERATES THE EXCEPTION
         /*  mExpandableList= (ExpandableListView) this.findViewById(R.id.expandable_list);

         ExpandableListAdapter adapter =  (ExpandableListAdapter) mExpandableList.getExpandableListAdapter();
           ArrayList<String>  arrayChildren = new ArrayList<String>();
           ArrayList<parent>  arrayParents = new ArrayList<parent>();
           parent parent1=new parent();
           parent parent2=new parent();
           parent parent3=new parent();
           parent parent4=new parent();

            parent1 = ((parent)adapter.getGroup(0));
            arrayChildren=parent1.getArrayChildren();
            arrayChildren.add("share4");  
            parent1.setArrayChildren(arrayChildren);
            arrayParents.add(parent1);

            parent2 = ((parent)adapter.getGroup(1));
            arrayParents.add(parent2);

            parent3 = ((parent)adapter.getGroup(2));
            arrayParents.add(parent3);

            parent4 = ((parent)adapter.getGroup(3));
            arrayParents.add(parent4);

          mExpandableList.setAdapter(new MyCustomAdapter(addshare.this,arrayParents));
         */ 
           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, MainActivity.class);
        startActivity(i);





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




 }

当我添加记录时,上面的代码给出了这个错误:LOGCAT:

 06-18 22:55:12.632: E/AndroidRuntime(681): FATAL EXCEPTION: main
 06-18 22:55:12.632: E/AndroidRuntime(681): java.lang.IllegalStateException: Could not  execute method of the activity
 06-18 22:55:12.632: E/AndroidRuntime(681):     at                        android.view.View$1.onClick(View.java:3039)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.view.View.performClick(View.java:3480)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.view.View$PerformClick.run(View.java:13983)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.os.Handler.handleCallback(Handler.java:605)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.os.Handler.dispatc hMessage(Handler.java:92)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.os.Looper.loop(Looper.java:137)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.app.ActivityThread.main(ActivityThread.java:4340)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at java.lang.reflect.Method.invoke(Method.java:511)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at com .android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at dalvik.system.NativeStart.main(Native Method)
 06-18 22:55:12.632: E/AndroidRuntime(681): Caused by: java.lang.reflect.InvocationTargetException
 06-18 22:55:12.632: E/AndroidRuntime(681):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at java.lang.reflect.Method.invoke(Method.java:511)
 06-18 22:55:12.632: E/AndroidRuntime(681):     at android.view.View$1.onClick(View.java:3034)
 06-18 22:55:12.632: E/AndroidRuntime(681):     ... 11 more
 06-18 22:55:12.632: E/AndroidRuntime(681): Caused by: java.lang.NullPointerException
 06-18 22:55:12.632: E/AndroidRuntime(681):     at com.example.moolah.addshare.addingshare(addshare.java:58)
 06-18 22:55:12.632: E/AndroidRuntime(681):     ... 14 more

可扩展列表 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"
           >

   <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>

4

1 回答 1

1

mExpandableList为空,因此this.findViewById(R.id.expandable_list)无法找到视图。

于 2013-06-18T17:53:46.203 回答