1

我编写了一个非常小的代码来通过捆绑在两个活动之间传递数据。现在我想我已经遵循了正确的步骤,但是代码给出了错误。如果这是一个非常愚蠢的错误,请原谅我,但我被卡住了,我对这段代码视而不见,请帮助我。这是代码..

package veniteck.solutions.mapmymetro;

    public class MainActivity extends Activity 
    {
    ImageView findnearestsource, findnearestdestination, getroute;
    String[] stops = {
            "Bangalore International Exhibition Center", "Jindal", "Manjunathnagar", "Nagasandra", "Dasarahalli", "Jalahalli", "Peenya Industry", "Peenya", "Yeswanthpur Industry", "Yeswanthpur", "Sandal Soap Factory", "Mahalaxmi", "Rajajinagar", "Kuvempu Road", "Srirampura", "Sampige Road", "Kempegowda Interchange", "Chikpet", "K R Market", "National College", "Lalbagh", "South End Circle", "Jayanagar", "R V Road Interchange", "Banashankari", "J P Nagar", "Puttenahalli", "Anjanapura Cross Road", "Krishna Leela Park", "Vajrahalli", "Thaighattapura", "Anjanapura/NICE Junction", "Kengeri", "R V College of Engineering", "Bangalore University Cross", "Rajarajeshwari Nagar", "Nayandahalli", "Mysore Road", "Deepanjali Nagar", "Attiguppe", "Vijayanagar", "Hosahall1i", "Magadi Road", "Sir M Vishweshwariah", "Vidhana Soudha", "M G Road Interchange", "Trinity", "Halasuru", "Indiranagar", "S V Road", "Baiyyappanahalli", "Jyotipura", "K R Puram", "Mahadevpura", "Garudacharpalya", "Doddanekkundi Induatrial State", "Vishweshwariah Industrial State", "Kundanahalli", "Vydhehi Hospital", "Satya Sai Medical Institute", "ITPB", "Kadugodi Industrial Area", "Ujjwal Vidhyalaya", "Whitefield", "Nagawara", "Arabic College", "Venkateshpura", "Tannery Town", "Pottery Town", "Cantonment Railway Station", "Shivajinagar", "Vellara Junction", "Langford Town", "Mico Bosch", "Dairy Circle", "Swagath Road Cross", "Jayadeva Hospital Interchange", "J P Nagar 4th Phase", "IIMB", "Hulimavu", "Gottigere", "Ragigudda Temple", "BTM Layout", "Silk Board", "HSR Layout", "Oxford College", "Muneshwara Nagar", "Chikkabegur", "Basapura Road", "Hosa Road", "Electronics City 1", "Electronics City 2", "Huskur Road", "Hebbagodi", "Bommasandra"
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AutoCompleteTextView source = (AutoCompleteTextView)findViewById(R.id.autofillsource);
        source.setAdapter(new ArrayAdapter<String>(this, R.layout.list_details, stops));

        AutoCompleteTextView destination = (AutoCompleteTextView)findViewById(R.id.autofilldestination);
        destination.setAdapter(new ArrayAdapter<String>(this, R.layout.list_details, stops));

        findnearestsource = (ImageView)findViewById(R.id.findnearestsource);
        findnearestsource.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Please Wait..", Toast.LENGTH_LONG).show();

            }
        });

        findnearestdestination = (ImageView)findViewById(R.id.findnearestdestination);
        findnearestdestination.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Please Wait..", Toast.LENGTH_LONG).show();

            }
        });

        getroute = (ImageView)findViewById(R.id.getroute);
        getroute.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Please Wait..", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this,Route.class);
                startActivity(intent);

            }   
        });



         // SECTION FOR BUNDLE CODE

        String src = source.getText().toString();
        String dest = destination.getText().toString();


         Bundle b = new Bundle();

         b.putString("srcstop", src);
         b.putString("deststop", dest);

         Intent in = new Intent(getApplicationContext(), Route.class);
         in.putExtras(b);
         startActivity(in);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    }

其他活动

路由.java

    public class Route  extends Activity 
    {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.routemap);



         // SECTION FOR BUNDLE CODE



          TextView RouteText = (TextView)findViewById(R.id.RouteText);

          Intent in = getIntent();

          //Getting bundle
          Bundle b = in.getExtras();

          //Getting data from bundle
          String Source = b.getString("srcstop");
          String Destination = b.getString("deststop");

          //Binding values to TextViews
           RouteText.setText("Getting route directions from " +Source +"to " +Destination);


    }
    }

这是Logcat输出..

日志猫

文本格式的 Logcat

10-04 12:38:13.722: D/dalvikvm(972): GC_FOR_ALLOC freed 0K, 4% free 8173K/8455K, paused 26ms, total 26ms
10-04 12:38:13.732: I/dalvikvm-heap(972): Grow heap (frag case) to 8.881MB for 896016-byte allocation
10-04 12:38:13.852: D/dalvikvm(972): GC_CONCURRENT freed 0K, 4% free 9048K/9351K, paused 74ms+14ms, total 120ms
10-04 12:38:13.922: I/Choreographer(972): Skipped 144 frames!  The application may be doing too much work on its main thread.
10-04 12:38:14.032: D/gralloc_goldfish(972): Emulator without GPU emulation detected.
10-04 12:38:14.381: I/Choreographer(972): Skipped 56 frames!  The application may be doing too much work on its main thread.
10-04 12:38:26.531: I/Choreographer(972): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-04 12:38:37.222: D/AndroidRuntime(972): Shutting down VM
10-04 12:38:37.222: W/dalvikvm(972): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-04 12:38:37.242: E/AndroidRuntime(972): FATAL EXCEPTION: main
10-04 12:38:37.242: E/AndroidRuntime(972): java.lang.RuntimeException: Unable to start activity ComponentInfo{veniteck.solutions.mapmymetro/veniteck.solutions.mapmymetro.Route}: java.lang.NullPointerException
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.os.Looper.loop(Looper.java:137)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 12:38:37.242: E/AndroidRuntime(972):  at java.lang.reflect.Method.invokeNative(Native Method)
10-04 12:38:37.242: E/AndroidRuntime(972):  at java.lang.reflect.Method.invoke(Method.java:511)
10-04 12:38:37.242: E/AndroidRuntime(972):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 12:38:37.242: E/AndroidRuntime(972):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 12:38:37.242: E/AndroidRuntime(972):  at dalvik.system.NativeStart.main(Native Method)
10-04 12:38:37.242: E/AndroidRuntime(972): Caused by: java.lang.NullPointerException
10-04 12:38:37.242: E/AndroidRuntime(972):  at veniteck.solutions.mapmymetro.Route.onCreate(Route.java:25)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.Activity.performCreate(Activity.java:5008)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-04 12:38:37.242: E/AndroidRuntime(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-04 12:38:37.242: E/AndroidRuntime(972):  ... 11 more

当我在第一个活动的文本视图中没有输入任何内容时 在此处输入图像描述

我可以去Route.java。

在此处输入图像描述

但是当我输入一些东西时, 在此处输入图像描述

然后点击获取路线,它崩溃了。 在此处输入图像描述

如果这个问题愚蠢到不能上SO,真的很抱歉。但我真的没有得到解决方案。

4

3 回答 3

1

尝试在 mainActivity 的 onclick() 中编写以下代码:

        String src = source.getText().toString();
        String dest = destination.getText().toString();


         Bundle b = new Bundle();

         b.putString("srcstop", src);
         b.putString("deststop", dest);

         Intent in = new Intent(getApplicationContext(), Route.class);
         in.putExtras(b);
         startActivity(in);
于 2013-10-04T07:27:36.983 回答
1

你需要把这些移到里面 onClick。改成

getroute.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          Toast.makeText(getApplicationContext(), "Please Wait..", Toast.LENGTH_LONG).show();
          String src = source.getText().toString();
          String dest = destination.getText().toString();
          Bundle b = new Bundle();
          b.putString("srcstop", src);
          b.putString("deststop", dest);
          Intent in = new Intent(MainActivity.this, Route.class);
          in.putExtras(b);
          startActivity(in);

        }   
    });

以下是外部点击监听器。所以字符串为空。移到里面onClick

String src = source.getText().toString();
String dest = destination.getText().toString();

同时进行这些更改

final AutoCompleteTextView source = (AutoCompleteTextView)findViewById(R.id.autofillsource);
final AutoCompleteTextView destination = (AutoCompleteTextView)findViewById(R.id.autofilldestination);
于 2013-10-04T07:36:47.330 回答
1

你的代码在点击监听器之外。让它在点击监听器里面,它应该可以工作..

String src = source.getText().toString();
String dest = destination.getText().toString();
于 2013-10-17T04:30:45.090 回答