-2

我编写了有两个类的程序,但是当我点击地图按钮时,程序已经停止。当我删除构造函数时,程序正确运行!我编写了主程序的一部分。

位置.java:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

    initButtons();

    //Not complete this method
    text_location.setOnKeyListener(new OnKeyListener(){
        public boolean onKey(View view, int keyCode,KeyEvent event){
            if(keyCode==KeyEvent.KEYCODE_ENTER){
                Geocoder geo=new  Geocoder(getApplicationContext(),Locale.getDefault());

                List<Address> address;
                try {
                    address = geo.getFromLocationName(text_location.getText().toString(),3);
                    if(address.size()>0){
                        searchLocation=new GeoPoint((int)(address.get(0).getLatitude()*1e6),(int)(address.get(0).getLongitude()*1e6));

                        controller.animateTo(searchLocation);
                        controller.setZoom(12);

                        mapOverlays=map.getOverlays();
                        drawable=getResources().getDrawable(R.drawable.marker);
                        itemizedOverlay=new SimpleItemizedOverlay(drawable,map);
                            textLocation=text_location.getText().toString();
                        //

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return true;
            }
            return false;
        }
    });
}
private void initButtons(){
    listButton=(Button)findViewById(R.id.button_list);
    listButton.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){

    public void onClick(View v){
    case R.id.button_list:
        ListLocation listLocation=new ListLocation(textLocation);
        Intent k=new Intent(Location.this,ListLocation.class);
        startActivity(k);
        break;

    }
 }
};

ListLocation.java

public class ListLocation extends Activity{
TextView text_location;
String textLocation="NOTHING";

public ListLocation(String text){
    textLocation=text;
} 
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_location);


    text_location=(TextView)findViewById(R.id.list_location);
    text_location.setText(textLocation);

}
}

日志猫:

11-12 13:22:41.959: W/dalvikvm(26132): threadid=1: thread exiting with uncaught exception (group=0x40a891f8)
11-12 13:22:41.966: E/AndroidRuntime(26132): FATAL EXCEPTION: main
11-12 13:22:41.966: E/AndroidRuntime(26132): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.ListLocation}: java.lang.InstantiationException: can't instantiate class org.example.loyaltier.ListLocation; no empty constructor
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.os.Looper.loop(Looper.java:137)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at dalvik.system.NativeStart.main(Native Method)
11-12 13:22:41.966: E/AndroidRuntime(26132): Caused by: java.lang.InstantiationException: can't instantiate class org.example.loyaltier.ListLocation; no empty constructor
11-12 13:22:41.966: E/AndroidRuntime(26132):    at java.lang.Class.newInstanceImpl(Native Method)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at java.lang.Class.newInstance(Class.java:1319)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-12 13:22:41.966: E/AndroidRuntime(26132):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
11-12 13:22:41.966: E/AndroidRuntime(26132):    ... 11 more
11-12 13:22:43.591: D/dalvikvm(26132): GC_CONCURRENT freed 265K, 3% free 15269K/15623K, paused 7ms+2ms
11-12 13:22:43.607: I/Process(26132): Sending signal. PID: 26132 SIG: 9

谢谢。干杯

4

1 回答 1

1

您不能以这种方式将值传递给活动,它们应该有空的构造函数..为了实现您的目标,请使用意图..

Intent k=new Intent(Location.this,ListLocation.class);
k.putExtra("Location", textLocation);
startActivity(k);

ListLocation.java

public class ListLocation extends Activity{
TextView text_location;
String textLocation="NOTHING";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_location);


    text_location=(TextView)findViewById(R.id.list_location);
    text_location.setText(getIntent().getStringExtra("Location"));
}
}
于 2012-11-12T10:07:50.060 回答