gpt4 book ai didi

java - 单击 ListView 中的项目会使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 12:14:50 26 4
gpt4 key购买 nike

我有问题。当我单击 ExpandableListView 中的项目时,我的应用程序崩溃了。我不知道问题出在哪里。在 Manifest.xml 中,我添加了 ButelkaPlastikowa.java。有任何想法吗?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);
}
}

我的代码: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;

}

}

日志

 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)
<小时/>

我的 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>

最佳答案

您遇到的问题是您的 xml 文件中没有 id 为“imageViewMap”的 View 。

这会导致 findViewById(R.id.imageViewMap); 返回 null,然后您尝试在 null 对象上调用函数。

我不确定您要在哪个 View 上设置 OnClickListener,但您需要添加 android:id="@+id/imageViewMap"到 xml 中的该 View 。

关于java - 单击 ListView 中的项目会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27081875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com