Pequeño proyecto para realizar el funcionamiento correcto antes de meterlo
en la aplicación gorda, bueno:
Al pulsar un botón o un item del menú me salga un layoutinflater y dentro de ese layoutinflater se muestra una lista simple.
Me muestra el poppupwindow correctamente pero no el listview y me da error en: vlista.setAdapter(adaptador) con el error java.lang.nullpointerexception.
Espero que me podáis ayudar y muchas gracias pase lo que pase. Un saludo
Pego aquí el código.
Activity_main.xml Código HTML:
<RelativeLayout 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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="28dp"
android:text="Button" />
</RelativeLayout>
inflater.xml Código HTML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444"
android:gravity="center_horizontal|top"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="298dp"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
MainActivity.class
Código:
package com.example.layoutinflater;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
public class MainActivity extends Activity implements OnClickListener {
private Button boton;
private ListView vLista;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton = (Button) findViewById(R.id.button1);
boton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
initiatePopupWindow();
break;
default:
break;
}
}
private PopupWindow pw;
private void initiatePopupWindow() {
String[] l = { "hola", "hola", "hola", "hola", "hola", "hola", "hola",
"hola", "hola", "hola", "hola" };
try {
// We need to get the instance of the LayoutInflater, use the
// context of this activity
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.inflater,
(ViewGroup) findViewById(R.id.screen));
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 300, 470, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this.getApplicationContext(),
android.R.layout.simple_list_item_1, l);
vLista = (ListView) findViewById(R.id.listView1);
vLista.setAdapter(adaptador);
} catch (Exception e) {
e.printStackTrace();
}
}
}
e.prinstacke(ERROR)
01-16 02:37:51.339: W/System.err(5500): java.lang.NullPointerException
01-16 02:37:51.339: W/System.err(5500): at com.example.layoutinflater.MainActivity.initiatePo pupWindow(MainActivity.java:70)
01-16 02:37:51.339: W/System.err(5500): at com.example.layoutinflater.MainActivity.onClick(Ma inActivity.java:42)
01-16 02:37:51.339: W/System.err(5500): at android.view.View.performClick(View.java:3534)
01-16 02:37:51.339: W/System.err(5500): at android.view.View$PerformClick.run(View.java:14263 )
01-16 02:37:51.339: W/System.err(5500): at android.os.Handler.handleCallback(Handler.java:605 )
01-16 02:37:51.339: W/System.err(5500): at android.os.Handler.dispatchMessage(Handler.java:92 )
01-16 02:37:51.339: W/System.err(5500): at android.os.Looper.loop(Looper.java:137)
01-16 02:37:51.339: W/System.err(5500): at android.app.ActivityThread.main(ActivityThread.jav a:4441)
01-16 02:37:51.339: W/System.err(5500): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 02:37:51.339: W/System.err(5500): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 02:37:51.339: W/System.err(5500): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:784)
01-16 02:37:51.339: W/System.err(5500): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:551)
01-16 02:37:51.339: W/System.err(5500): at dalvik.system.NativeStart.main(Native Method)