从非活动类调用PopupListView类

时间:2016-02-11 16:10:12

标签: android listview popupmenu

我开发了一个popupListView类,我想通过另一个不调用活动的类来调用它,但是我收到错误"Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference",有没有人知道如何解决它? 以下是我的代码的一些部分:

popup.java

    public class PopUp extends Activity {
    private static final String TAG = "popup test" ;
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
            "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
    ListView myList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        test();
    }



    public void test(){

        Log.i(TAG, "I'm test for popup");
        setContentView(R.layout.popupactivity);
        myList = (ListView) findViewById(R.id.listView);
        myList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listview_array));
        Helper.getListViewSize(myList);
    }

    public void tesssst(){
        Log.i(TAG, "I'm test for popup");
    }
}

popup.xml

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PopUp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="19dp"
            android:text="@string/hello_world" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_launcher" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</ScrollView>

另一类方法:

    handle_popup(){
         PopUp popup = new PopUp();
            popup.test();
}

1 个答案:

答案 0 :(得分:0)

我故意解决了这个问题:

handle_popup(){Intent i = new Intent(this, PopUp.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(i);}

不要忘记在清单中添加以下代码:

<activity android:name="com.spd.spdsharareh.PopUp"></activity>