我想从另一个listView创建listView时出错

时间:2013-04-30 20:14:33

标签: android

我有两节课。在第一个类ListDataActivityInterv中,我想显示ListViewMultipleSelectionActivity类中的所有项。所以我在第一堂课,当我按下按钮添加我想要显示一个listView(第二类),检查一个,两个项目,当我按下发送按钮出现在第一个班级。另外,我想将这些项目存储在表格中。这是第一堂课的代码:

public class ListDataActivityInterv extends Activity{

    private ListView lsInterventiiView = null;
    private PacientiDB pacientiDB = null;
    private PacientiAdapter adapter = null;
    private ArrayList<Interventii> listInterventii = null;
    private Button buttonCreate2 = null;
    private String text = null;

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

         text = getIntent().getExtras().getString("Pacienti");
         TextView textView1 = (TextView) findViewById(R.id.textView2);
         textView1.setText(text);

         buttonCreate2 = (Button) findViewById(R.id.btnCreateInt);
         buttonCreate2.setOnClickListener(new OnClickListener() {

             @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ListDataActivityInterv.this, ListViewMultipleSelectionActivity.class);
                    startActivity(intent);
                }
        });

            Bundle b = getIntent().getExtras();        
            String[] resultArr = b.getStringArray("selectedItems");       
            ListView lv = (ListView) findViewById(R.id.lsvListInterventii);        
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);        
            lv.setAdapter(adapter);  
                    }

}

第二节课:

public class ListViewMultipleSelectionActivity extends Activity implements
        OnClickListener {   
        Button button;   
        ListView listView;    
        ArrayAdapter<String> adapter; 
        public String text = null;
        private PacientiDB pacientiDB = null;

        /** Called when the activity is first created. */    
        @Override    
        public void onCreate(Bundle savedInstanceState) {       
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.maincheckinterv);  
            text = getIntent().getExtras().getString("Pacienti");
            TextView text1 = (TextView) findViewById(R.id.textViewListInter);
            text1.setText(text);
            findViewsById();         
            String[] sports = getResources().getStringArray(R.array.sports_array);       
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, sports);     
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);      
            listView.setAdapter(adapter);        
            button.setOnClickListener(this);    }    

        private void findViewsById() {        
            listView = (ListView) findViewById(R.id.list);       
            button = (Button) findViewById(R.id.testbutton);    }    

        public void onClick(View v) {    
            Interventii interventie = pacientiDB.getInterv(text);
            SparseBooleanArray checked = listView.getCheckedItemPositions();       
            ArrayList<String> selectedItems = new ArrayList<String>();        
            for (int i = 0; i < checked.size(); i++) {           

                int position = checked.keyAt(i);            

                if (checked.valueAt(i))                
                    selectedItems.add(adapter.getItem(position));        }       
            String[] outputStrArr = new String[selectedItems.size()];         
            for (int i = 0; i < selectedItems.size(); i++) {           
                outputStrArr[i] = selectedItems.get(i);  
                interventie = new Interventii();
                interventie.setDenumire(selectedItems.get(i));
                pacientiDB.insertI(interventie);
                }         
            Intent intent = new Intent(getApplicationContext(), ListDataActivityInterv.class);                
            Bundle b = new Bundle();       
            b.putStringArray("selectedItems", outputStrArr);             
            intent.putExtras(b);               
            startActivity(intent);    }
        }

我收到此错误:

04-30 19:42:55.642: E/AndroidRuntime(4899): FATAL EXCEPTION: main
04-30 19:42:55.642: E/AndroidRuntime(4899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.medicaldoi/com.example.medicaldoi.ListDataActivityInterv}: java.lang.NullPointerException
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.os.Looper.loop(Looper.java:123)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at java.lang.reflect.Method.invoke(Method.java:521)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at dalvik.system.NativeStart.main(Native Method)
04-30 19:42:55.642: E/AndroidRuntime(4899): Caused by: java.lang.NullPointerException
04-30 19:42:55.642: E/AndroidRuntime(4899):     at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at java.util.Arrays.asList(Arrays.java:171)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at com.example.medicaldoi.ListDataActivityInterv.onCreate(ListDataActivityInterv.java:45)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 19:42:55.642: E/AndroidRuntime(4899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-30 19:42:55.642: E/AndroidRuntime(4899):     ... 11 more

有人能帮助我吗?有人有另一个想法吗?

3 个答案:

答案 0 :(得分:1)

您的String数组为空

        Bundle b = getIntent().getExtras();        
        String[] resultArr = b.getStringArray("selectedItems");  
        if(resultArr==null)
        finish();     
        ListView lv = (ListView) findViewById(R.id.lsvListInterventii);        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);        
        lv.setAdapter(adapter);

现在检查你的应用程序是否直接关闭,你的字符串数组为空。??

你也可以检查bundle是否有字符串数组.....如果它有数组而不是null。

b.containsKey(resultArr)

答案 1 :(得分:0)

ListView listView行;在您的第二个类中创建对listView的引用,但是,您尚未实例化此对象。在某些时候,您需要执行以下操作;

listView = new ListView(getApplicationContext());

或者从该上下文中可用的另一个列表视图中分配对它的引用。

答案 2 :(得分:0)

使用以下类,ListDataActivityInterv

package com.pranav.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ListDataActivityInterv extends Activity{

    private ListView lsInterventiiView = null;
   // private PacientiDB pacientiDB = null;
    //private PacientiAdapter adapter = null;
   // private ArrayList<Interventii> listInterventii = null;
    Button button ;
    String text = null;
    ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewinterventii);
        lv = (ListView) findViewById(R.id.listView1);        


         //text = getIntent().getExtras().getString("Pacienti");
         //TextView textView1 = (TextView) findViewById(R.id.textView2);
        // textView1.setText(text);

         button = (Button) findViewById(R.id.button1);
         button.setOnClickListener(new OnClickListener() {

             @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ListDataActivityInterv.this, ListViewMultipleSelectionActivity.class);
                    startActivity(intent);
                    finish();
                }
        });
         Bundle b = getIntent().getExtras(); 
            if(b!=null && b.containsKey("selectedItems")){

            String[] resultArr = b.getStringArray("selectedItems");       
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);        
            lv.setAdapter(adapter); } 
                    }

}

这是ListViewMultipleSelectionActivity。

package com.pranav.listviewdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class ListViewMultipleSelectionActivity extends Activity implements  OnClickListener {

    Button button;
    ListView listView;
    ArrayAdapter<String> adapter;
    public String text = null;
    //private PacientiDB pacientiDB = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maincheckinterv);
        //text = getIntent().getExtras().getString("Pacienti");
        //TextView text1 = (TextView) findViewById(R.id.textViewListInter);
        //text1.setText(text);
        findViewsById();
        String[] sports = new String[]{"Cricket","football","game1","chess"};
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, sports);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);
        button.setOnClickListener(this);
    }

    private void findViewsById() {
        listView = (ListView) findViewById(R.id.listView2);
        button = (Button) findViewById(R.id.button2);
    }

    public void onClick(View v) {
        //Interventii interventie = pacientiDB.getInterv(text);
        SparseBooleanArray checked = listView.getCheckedItemPositions();
        ArrayList<String> selectedItems = new ArrayList<String>();
        for (int i = 0; i < checked.size(); i++) {

            int position = checked.keyAt(i);

            if (checked.valueAt(i))
                selectedItems.add(adapter.getItem(position));
        }
        String[] outputStrArr = new String[selectedItems.size()];
        for (int i = 0; i < selectedItems.size(); i++) {
            outputStrArr[i] = selectedItems.get(i);
        //  interventie = new Interventii();
        //  interventie.setDenumire(selectedItems.get(i));
        //  pacientiDB.insertI(interventie);
        }
        Intent intent = new Intent(getApplicationContext(),
                ListDataActivityInterv.class);
        Bundle b = new Bundle();
        b.putStringArray("selectedItems", outputStrArr);
        intent.putExtras(b);
        startActivity(intent);
    }
}

layout:-listviewinterventii.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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

布局:-maincheckinterv.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" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="390dp" >
    </ListView>



</LinearLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pranav.listviewdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pranav.listviewdemo.ListDataActivityInterv"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.pranav.listviewdemo.ListViewMultipleSelectionActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>