无法在android中创建列表视图

时间:2014-04-22 10:49:32

标签: java android android-listview

我将字符串数组传递给从一个活动到另一个活动的包我想在第二个活动的列表视图中显示这个字符串数组。但是没有创建列表视图。以下是相同的代码:

// welcome.java

package com.example.eleave;
import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Welcome extends Activity {
       int l;
    Button b1,b2;
    EditText e1,e2;
     String result2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        b1=(Button) findViewById(R.id.button1);
        b2=(Button) findViewById(R.id.button2);
        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if(e1.getText().length()==0 ||e2.getText().length()==0)
                {
                    Toast.makeText(getBaseContext(), "Please Enter all the fields", Toast.LENGTH_LONG).show();
                }
                else{
                    String username=e1.getText().toString();
                String password=e2.getText().toString();
                AsyncTask<String, Void, String> s=new SignUp(Welcome.this,username,password).execute(username,password);
                try{
        String result=s.get();
         String password1=password+"0";
           String password2=password+"1";
       if(result.equals(password1))
       {
            Toast.makeText(getBaseContext(),"student", Toast.LENGTH_LONG).show();
            Intent i=new Intent(Welcome.this,LeaveApp.class);
             Bundle b=new Bundle();
                b.putString("username", username);
                i.putExtras(b);
            startActivity(i);
       }
       else if(result.equals(password2))
       {
String usernam[]=new String[20];
            String phone_no1[]=new String[20];
            String reason[]=new String[20];
            String sem[]=new String[20];
            String from_date[]=new String[20];
            String to_date[]=new String[20];
          // Toast.makeText(getBaseContext(), "teacher", Toast.LENGTH_LONG).show();
           AsyncTask<String,Void,String>id1=new CountRows(Welcome.this).execute();
           try{
               result2= id1.get();
                l=Integer.parseInt(result2);
           }catch(Exception e)
           {

           }
           AsyncTask<String,Void,String>id=new Upload(Welcome.this).execute();
           try{
              String result1= id.get();
            int j=0;
            for(int i=0;i<l;i++)
            {
        String div[]=result1.split("-");
         phone_no1[i]=div[j++];
         reason[i]=div[j++];
         sem[i]=div[j++];
         from_date[i]=div[j++];
         to_date[i]=div[j++];
        // Toast.makeText(getBaseContext(), phone_no1[i], Toast.LENGTH_LONG).show();

         AsyncTask<String,Void,String>id3=new GetUser(Welcome.this).execute(phone_no1[i]);
           try{
              String result3= id3.get();
              usernam[i]=result3;
              }catch(Exception e)
              {

              }



            }
             Toast.makeText(getBaseContext(), phone_no1[0]+reason[0]+sem[0]+from_date[0]+to_date[0], Toast.LENGTH_LONG).show();
        // Toast.makeText(getBaseContext(),result1, Toast.LENGTH_LONG).show();
           }catch(Exception e)
           {

           }
           Intent i=new Intent(Welcome.this,View_Stud.class);

               Bundle bundle =new Bundle();
               bundle.putStringArray("phoneno",phone_no1);
               bundle.putStringArray("reason",reason);
               bundle.putStringArray("sem",sem);
               bundle.putStringArray("from_date",from_date);
               bundle.putStringArray("to_date",to_date);
                bundle.putStringArray("username", usernam);
                bundle.putInt("length", l);
                i.putExtras(bundle);
                startActivity(i);
           }

        else
        {
            Toast.makeText(getBaseContext(),"INVALID USERNAME OR PASSWORD", Toast.LENGTH_LONG).show();

        }
                }catch(Exception e)
                {

                }}

            }
        });
        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 Intent i=new Intent(Welcome.this,MainActivity.class);
                    startActivity(i);
            }
        });

    }

}

// view_stud.java

package com.example.eleave;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class View_Stud extends Activity{



    String[] phno,reason,sem,fdate,tdate;
    String[] uname;
    int len;
    ListView lv1;
    String str[]=new String[30];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_stud);
         Bundle bundle = getIntent().getExtras();
           String phno[]= bundle.getStringArray("phoneno");
           String reason[]= bundle.getStringArray("reason");
           String sem[]=bundle.getStringArray("sem");
         String  fdate[]=bundle.getStringArray("from_date");
        String  tdate[]= bundle.getStringArray("to_date");
      String uname[]=bundle.getStringArray("username");
       int len=bundle.getInt("length");
         for(int i=0;i<len;i++)
         {
             str[i]=uname[i]+sem[i];
         }


        lv1=(ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);

        lv1.setAdapter(ad);
        lv1.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                Intent i=new Intent(getApplicationContext(),Approve_Stud.class);


                i.putExtra("data2", str[arg2]);
                startActivity(i);

            }

        });

    }
}

// logcat错误

04-22 16:34:37.420: E/AndroidRuntime(4439): FATAL EXCEPTION: main
04-22 16:34:37.420: E/AndroidRuntime(4439): java.lang.NullPointerException
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.ListView.onMeasure(ListView.java:1127)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.View.measure(View.java:8314)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.View.measure(View.java:8314)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.View.measure(View.java:8314)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.View.measure(View.java:8314)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.View.measure(View.java:8314)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.os.Looper.loop(Looper.java:130)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at java.lang.reflect.Method.invoke(Method.java:507)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
04-22 16:34:37.420: E/AndroidRuntime(4439):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

检查您正在使用的ArrayAdapter的重载构造函数,其中一个作为项接受Array另一个ArrayList。另外我认为你的Array对象 str 存在语法错误,你还没有正确创建数组对象。您可以使用与数组类似的对象的数组列表,但您不必担心索引,因为ArrayList可以动态更改其容量

答案 1 :(得分:0)

str[]初始化为

 int len=bundle.getInt("length");

 String str[]=new String[len];

     for(int i=0;i<len;i++)
     {
         str[i]=uname[i]+sem[i];
     }