Reputation: 65
I have problem. When I click on item from expandableListView then my app crash. I don't know where is problem. In Manifest.xml I have add ButelkaPlastikowa.java. Any ideas? SegregateWasteActivity.java
public class SegregateWasteActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Alphabet> alphabetList = new ArrayList<Alphabet>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.segregate_waste_activity);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
displayList();
expandAll();
myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView elv, View view,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
TextView tv = (TextView) view.findViewById(R.id.name);
String name = (String) tv.getText();
try{
if(name.contains("Akumulatory samochodowe")){
Intent intent1 = new Intent(SegregateWasteActivity.this, Akumulatory.class);
startActivity(intent1);
}
else if (name.contains("Armatura sanitarna")){
Intent intent1 = new Intent(SegregateWasteActivity.this, Armatura.class);
startActivity(intent1);
}
else if (name.contains("Artykuły higieniczne")){
Intent intent1 = new Intent(SegregateWasteActivity.this, Higiena.class);
startActivity(intent1);
}
else if (name.contains("Artykuły zawierające rtęć")){
Intent intent1 = new Intent(SegregateWasteActivity.this, Rtec.class);
startActivity(intent1);
}
else if (name.contains("Butelka plastikowa")){
Intent intent1 = new Intent(SegregateWasteActivity.this, ButelkaPlastikowa.class);
startActivity(intent1);
}
Class cls = Class.forName("com.odpad.odpadygdansk.waste." + name);
Intent intent = new Intent(SegregateWasteActivity.this, cls);
startActivity(intent);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
return false;
}
});
}
@Override
public boolean onClose() {
// TODO Auto-generated method stub
listAdapter.filterData("");
expandAll();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
listAdapter.filterData(query);
expandAll();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
listAdapter.filterData(newText);
expandAll();
return false;
}
private void expandAll()
{
int count = listAdapter.getGroupCount();
for(int i = 0; i < count; i++)
{
myList.expandGroup(i);
}
}
private void displayList()
{
loadSomeData();
myList = (ExpandableListView) findViewById(R.id.expandableList);
listAdapter = new MyListAdapter(SegregateWasteActivity.this, alphabetList);
myList.setAdapter(listAdapter);
}
private void loadSomeData()
{
ArrayList<Waste> wasteList = new ArrayList<Waste>();
Waste waste = new Waste("Aerozol");
wasteList.add(waste);
waste = new Waste("Akumulatory samochodowe");
wasteList.add(waste);
waste = new Waste("Armatura sanitarna");
wasteList.add(waste);
waste = new Waste("Artykuły higieniczne");
wasteList.add(waste);
waste = new Waste("Artykuły zawierające rtęć");
wasteList.add(waste);
Alphabet alphabet = new Alphabet("A", wasteList);
alphabetList.add(alphabet);
wasteList = new ArrayList<Waste>();
waste = new Waste("Bateria");
wasteList.add(waste);
waste = new Waste("Butelka plastikowa");
wasteList.add(waste);
alphabet = new Alphabet("B", wasteList);
alphabetList.add(alphabet);
}
}
My code: ButelkaPlastikowa.java
public class ButelkaPlastikowa extends Activity{
ImageView mapOfWaste;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to create game.xml
setContentView(R.layout.plastic_bottle);
TextView plastic_bottle = (TextView) findViewById(R.id.TextViewPlasticBottle);
plastic_bottle.setText(Html.fromHtml(getString(R.string.plastic_bottle)));
mapOfWasteActivity();
}
/** Called when the user clicks the ImageView */
public void mapOfWasteActivity() {
mapOfWaste = (ImageView)findViewById(R.id.imageViewMap);
mapOfWaste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isNetworkConnected()){
Intent intent = new Intent(ButelkaPlastikowa.this, MapOfWasteActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(ButelkaPlastikowa.this,NoInternet.class);
startActivity(intent);
}
}
});
}
public boolean isNetworkConnected(){
boolean isConnected = false;
try{
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
return isConnected;
}
catch(Exception e)
{
e.printStackTrace();
}
return isConnected;
}
}
Log
11-22 20:24:19.513: E/AndroidRuntime(32357): FATAL EXCEPTION: main
11-22 20:24:19.513: E/AndroidRuntime(32357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.odpad.odpadygdansk/com.odpad.odpadygdansk.waste.ButelkaPlastikowa}: java.lang.NullPointerException
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread.access$600(ActivityThread.java:133)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.os.Looper.loop(Looper.java:137)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread.main(ActivityThread.java:4803)
11-22 20:24:19.513: E/AndroidRuntime(32357): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 20:24:19.513: E/AndroidRuntime(32357): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 20:24:19.513: E/AndroidRuntime(32357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-22 20:24:19.513: E/AndroidRuntime(32357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-22 20:24:19.513: E/AndroidRuntime(32357): at dalvik.system.NativeStart.main(Native Method)
11-22 20:24:19.513: E/AndroidRuntime(32357): Caused by: java.lang.NullPointerException
11-22 20:24:19.513: E/AndroidRuntime(32357): at com.odpad.odpadygdansk.waste.ButelkaPlastikowa.mapOfWasteActivity(ButelkaPlastikowa.java:35)
11-22 20:24:19.513: E/AndroidRuntime(32357): at com.odpad.odpadygdansk.waste.ButelkaPlastikowa.onCreate(ButelkaPlastikowa.java:30)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.Activity.performCreate(Activity.java:5008)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-22 20:24:19.513: E/AndroidRuntime(32357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
My plastic_bottle.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" >
<ImageView
android:id="@+id/dangerous_waste"
android:layout_width="match_parent"
android:layout_height="70dp"
android:src="@drawable/odpady_niebezpieczne" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="300dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/obraz_aerozol2"
android:layout_width="match_parent"
android:layout_height="70dp"
android:src="@drawable/aerozol2" />
<TextView
android:id="@+id/TextViewPlasticBottle"
android:layout_width="match_parent"
android:layout_height="800dp"
android:text="@string/plastic_bottle" />
</LinearLayout>
</ScrollView>
<ImageView
android:id="@+id/imageViewPinezka"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="mapOfWasteActivity"
android:src="@drawable/pinezka" />
</LinearLayout>
Upvotes: 0
Views: 650
Reputation: 24857
The problem you are having is that you do not have a view in your xml file with the id of "imageViewMap".
This causes findViewById(R.id.imageViewMap);
to return null and then you are trying to call a function on a null object.
I'm not sure which view you are trying to set the OnClickListener
on but you will need to add the android:id="@+id/imageViewMap"
to that view in the xml.
Upvotes: 1