我有一个expandablelistview。
以下全部内容是我的MainActivity.java
其中有2个错误,两个都在}。
我确信他们的位置得当。尝试更新软件等,但由于某种原因,我不能让错误消失。
它是最后一个}和返回假之前的那个}
代码从这里开始:package com.example.sk17;
import com.example.adapterr.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
}
return false;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("1 SAFETY PRECAUTIONS");
listDataHeader.add("2 MACHINE FAMILIARIZATION");
listDataHeader.add("3 MACHINE OPERATION");
listDataHeader.add("4 MAINTENANCE");
listDataHeader.add("5 TRANSPORTATION");
listDataHeader.add("6 SPECIFICATIONS");
listDataHeader.add("7 OPTIONAL EQUIPMENT");
listDataHeader.add("8 TROUBLESHOOTING");
listDataHeader.add("9 SPECIAL PROCEDURES");
// Adding child data
List<String> SAFETYPRECAUTIONS = new ArrayList<String>();
SAFETYPRECAUTIONS.add("1.1 GENERAL SAFETY INFORMATION");
SAFETYPRECAUTIONS.add("1.2 SAFETY PRECAUTIONS");
SAFETYPRECAUTIONS.add("1.3 PRE-START SAFETY");
SAFETYPRECAUTIONS.add("1.4 SAFETY DURING OPERATION");
SAFETYPRECAUTIONS.add("1.5 INSPECTION & MAINTENANCE SAFETY");
SAFETYPRECAUTIONS.add("1.6 BATTERY SAFETY");
SAFETYPRECAUTIONS.add("1.7 WARNING LABELS & DECALS");
SAFETYPRECAUTIONS.add("1.8 PROHIBITED MACHINE OPERATION");
SAFETYPRECAUTIONS.add("1.9 END OF WORK SHIFT SAFETY");
SAFETYPRECAUTIONS.add("1.10 TRANSPORTATION");
SAFETYPRECAUTIONS.add("1.11 TOWING THE MACHINE");
List<String> MACHINEFAMILIARIZATION = new ArrayList<String>();
MACHINEFAMILIARIZATION.add("2.1 FAMILIARIZATION");
MACHINEFAMILIARIZATION.add("2.2 GENERAL MACHINE NOMENCLATURE");
MACHINEFAMILIARIZATION.add("2.3 OPERATOR CAB NOMENCLATURE");
MACHINEFAMILIARIZATION.add("2.4 COMPONENT & CONTROLS NOMENCLATURE");
List<String> MACHINEOPERATION = new ArrayList<String>();
MACHINEOPERATION.add("3.1 BASIC MACHINE OPERATION");
List<String> MAINTENANCE = new ArrayList<String>();
MAINTENANCE.add("4.1 GENERAL INFORMATION");
MAINTENANCE.add("4.2 GENERAL SAFETY & PRECAUTIONS");
MAINTENANCE.add("4.3 LUBRICANT, FUEL & COOLANT SPECIFICATIONS");
MAINTENANCE.add("4.4 MAINTENANCE PARTS");
MAINTENANCE.add("4.5 ACCESSORY TOOLS");
MAINTENANCE.add("4.6 TORQUE SPECIFICATIONS FOR BOLTS & NUTS");
MAINTENANCE.add("4.7 TORQUE SPECIFICATIONS FOR JOINTS & HYDRAULIC HOSES");
MAINTENANCE.add("4.8 INSTALLING HYDRAULIC HOSE AND PIPE JOINT FITTINGS");
MAINTENANCE.add("4.9 RELEASING INTERNAL PRESSURE OF HYDRAULIC SYSTEM");
MAINTENANCE.add("4.10 BLEEDING AIR IN FUEL SYSTEM");
MAINTENANCE.add("4.11 REPLACING RUBBER CRAWLER BELT");
MAINTENANCE.add("4.12 INSPECTION & MAINTENANCE CHART");
MAINTENANCE.add("4.13 OTHER MAINTENANCE - PERFORMED WHEN REQUIRED");
MAINTENANCE.add("4.14 PRE-START (EVERY 8 HOURS) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.15 50-HOUR (WEEKLY) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.16 250-HOUR (3-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.17 500-HOUR (6-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.18 1000-HOUR (12-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.19 2000-HOUR INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.20 5000-HOUR INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.21 INSPECTION / MAINTENANCE CHECK LIST");
List<String> TRANSPORTATION = new ArrayList<String>();
TRANSPORTATION.add("5.1 PREPARING FOR MACHINE TRANSPORTATION.");
TRANSPORTATION.add("5.2 LIFTING THE MACHINE");
TRANSPORTATION.add("5.3 WEIGHT AND DIMENSION SPECIFICATIONS");
List<String> SPECIFICATIONS = new ArrayList<String>();
SPECIFICATIONS.add("6.1 GENERAL SPECIFICATIONS");
List<String> OPTIONALEQUIPMENT = new ArrayList<String>();
OPTIONALEQUIPMENT.add("7.1 OPERATION OF HYDRAULIC BREAKER AND NIBBLER");
OPTIONALEQUIPMENT.add("7.2 SAFETY PRECAUTIONS");
OPTIONALEQUIPMENT.add("7.3 PERIODIC INSPECTION AND MAINTENANCE INTERVAL");
List<String> TROUBLESHOOTING = new ArrayList<String>();
TROUBLESHOOTING.add("8.1 GENERAL TROUBLESHOOTING");
List<String> SPECIALPROCEDURES = new ArrayList<String>();
SPECIALPROCEDURES.add("9.1 MOVING A DISABLED MACHINE.");
SPECIALPROCEDURES.add("9.2 RELEASING LOCK ON TRAVEL MOTOR");
listDataChild.put(listDataHeader.get(0), SAFETYPRECAUTIONS); // Header, Child data
listDataChild.put(listDataHeader.get(1), MACHINEFAMILIARIZATION);
listDataChild.put(listDataHeader.get(2), MACHINEOPERATION);
listDataChild.put(listDataHeader.get(3), MAINTENANCE);
listDataChild.put(listDataHeader.get(4), TRANSPORTATION);
listDataChild.put(listDataHeader.get(5), SPECIFICATIONS);
listDataChild.put(listDataHeader.get(6), OPTIONALEQUIPMENT);
listDataChild.put(listDataHeader.get(7), TROUBLESHOOTING);
listDataChild.put(listDataHeader.get(8), SPECIALPROCEDURES);
}
}
答案 0 :(得分:2)
更改为
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
return false;
}
}; // statement ends with ;
这是一个声明,后面是显式初始化,必须以;
答案 1 :(得分:2)
您的大括号不正确,并且适当的缩进会向您显示:
private OnChildClickListener myListItemClicked = new OnChildClickListener() { // open 1
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // open 2
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
} // close 2
return false;
} // close 1
这意味着return false;
位于匿名内部类的类主体中,而应位于方法主体中:
private OnChildClickListener myListItemClicked = new OnChildClickListener() {1
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
return false;
}
};
答案 2 :(得分:0)
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
}
return false;
}
这应该是:
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
return false;
}
}
您在方法外使用了返回...
答案 3 :(得分:0)
这是正确的代码: -
package com.example.sk17;
import com.example.adapterr.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, activity0.class);
startActivity(Intent);
return false; // return statement added here
}
}; // semicolon added
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("1 SAFETY PRECAUTIONS");
listDataHeader.add("2 MACHINE FAMILIARIZATION");
listDataHeader.add("3 MACHINE OPERATION");
listDataHeader.add("4 MAINTENANCE");
listDataHeader.add("5 TRANSPORTATION");
listDataHeader.add("6 SPECIFICATIONS");
listDataHeader.add("7 OPTIONAL EQUIPMENT");
listDataHeader.add("8 TROUBLESHOOTING");
listDataHeader.add("9 SPECIAL PROCEDURES");
// Adding child data
List<String> SAFETYPRECAUTIONS = new ArrayList<String>();
SAFETYPRECAUTIONS.add("1.1 GENERAL SAFETY INFORMATION");
SAFETYPRECAUTIONS.add("1.2 SAFETY PRECAUTIONS");
SAFETYPRECAUTIONS.add("1.3 PRE-START SAFETY");
SAFETYPRECAUTIONS.add("1.4 SAFETY DURING OPERATION");
SAFETYPRECAUTIONS.add("1.5 INSPECTION & MAINTENANCE SAFETY");
SAFETYPRECAUTIONS.add("1.6 BATTERY SAFETY");
SAFETYPRECAUTIONS.add("1.7 WARNING LABELS & DECALS");
SAFETYPRECAUTIONS.add("1.8 PROHIBITED MACHINE OPERATION");
SAFETYPRECAUTIONS.add("1.9 END OF WORK SHIFT SAFETY");
SAFETYPRECAUTIONS.add("1.10 TRANSPORTATION");
SAFETYPRECAUTIONS.add("1.11 TOWING THE MACHINE");
List<String> MACHINEFAMILIARIZATION = new ArrayList<String>();
MACHINEFAMILIARIZATION.add("2.1 FAMILIARIZATION");
MACHINEFAMILIARIZATION.add("2.2 GENERAL MACHINE NOMENCLATURE");
MACHINEFAMILIARIZATION.add("2.3 OPERATOR CAB NOMENCLATURE");
MACHINEFAMILIARIZATION.add("2.4 COMPONENT & CONTROLS NOMENCLATURE");
List<String> MACHINEOPERATION = new ArrayList<String>();
MACHINEOPERATION.add("3.1 BASIC MACHINE OPERATION");
List<String> MAINTENANCE = new ArrayList<String>();
MAINTENANCE.add("4.1 GENERAL INFORMATION");
MAINTENANCE.add("4.2 GENERAL SAFETY & PRECAUTIONS");
MAINTENANCE.add("4.3 LUBRICANT, FUEL & COOLANT SPECIFICATIONS");
MAINTENANCE.add("4.4 MAINTENANCE PARTS");
MAINTENANCE.add("4.5 ACCESSORY TOOLS");
MAINTENANCE.add("4.6 TORQUE SPECIFICATIONS FOR BOLTS & NUTS");
MAINTENANCE.add("4.7 TORQUE SPECIFICATIONS FOR JOINTS & HYDRAULIC HOSES");
MAINTENANCE.add("4.8 INSTALLING HYDRAULIC HOSE AND PIPE JOINT FITTINGS");
MAINTENANCE.add("4.9 RELEASING INTERNAL PRESSURE OF HYDRAULIC SYSTEM");
MAINTENANCE.add("4.10 BLEEDING AIR IN FUEL SYSTEM");
MAINTENANCE.add("4.11 REPLACING RUBBER CRAWLER BELT");
MAINTENANCE.add("4.12 INSPECTION & MAINTENANCE CHART");
MAINTENANCE.add("4.13 OTHER MAINTENANCE - PERFORMED WHEN REQUIRED");
MAINTENANCE.add("4.14 PRE-START (EVERY 8 HOURS) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.15 50-HOUR (WEEKLY) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.16 250-HOUR (3-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.17 500-HOUR (6-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.18 1000-HOUR (12-MONTH) INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.19 2000-HOUR INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.20 5000-HOUR INSPECTION AND MAINTENANCE");
MAINTENANCE.add("4.21 INSPECTION / MAINTENANCE CHECK LIST");
List<String> TRANSPORTATION = new ArrayList<String>();
TRANSPORTATION.add("5.1 PREPARING FOR MACHINE TRANSPORTATION.");
TRANSPORTATION.add("5.2 LIFTING THE MACHINE");
TRANSPORTATION.add("5.3 WEIGHT AND DIMENSION SPECIFICATIONS");
List<String> SPECIFICATIONS = new ArrayList<String>();
SPECIFICATIONS.add("6.1 GENERAL SPECIFICATIONS");
List<String> OPTIONALEQUIPMENT = new ArrayList<String>();
OPTIONALEQUIPMENT.add("7.1 OPERATION OF HYDRAULIC BREAKER AND NIBBLER");
OPTIONALEQUIPMENT.add("7.2 SAFETY PRECAUTIONS");
OPTIONALEQUIPMENT.add("7.3 PERIODIC INSPECTION AND MAINTENANCE INTERVAL");
List<String> TROUBLESHOOTING = new ArrayList<String>();
TROUBLESHOOTING.add("8.1 GENERAL TROUBLESHOOTING");
List<String> SPECIALPROCEDURES = new ArrayList<String>();
SPECIALPROCEDURES.add("9.1 MOVING A DISABLED MACHINE.");
SPECIALPROCEDURES.add("9.2 RELEASING LOCK ON TRAVEL MOTOR");
listDataChild.put(listDataHeader.get(0), SAFETYPRECAUTIONS); // Header, Child data
listDataChild.put(listDataHeader.get(1), MACHINEFAMILIARIZATION);
listDataChild.put(listDataHeader.get(2), MACHINEOPERATION);
listDataChild.put(listDataHeader.get(3), MAINTENANCE);
listDataChild.put(listDataHeader.get(4), TRANSPORTATION);
listDataChild.put(listDataHeader.get(5), SPECIFICATIONS);
listDataChild.put(listDataHeader.get(6), OPTIONALEQUIPMENT);
listDataChild.put(listDataHeader.get(7), TROUBLESHOOTING);
listDataChild.put(listDataHeader.get(8), SPECIALPROCEDURES);
}
}