I am pretty new to both android and NFC. I am working on an NFC related application as a college project that reads the data from tags and lists them. Although I am able to do so, I am facing problems with intent where I am supposed to mail this list to user on button click.

Can someone please tell me where I am going wrong and help with a detailed step-by-step approach. Huge thanks..!!

Here's the WebServiceActivity:

public class WebServiceActivity extends Activity {

    Intent intent = getIntent();
    String studlist = intent.getStringExtra("studlist");

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.webservice);

        TextView mailtext = (TextView) findViewById(R.id.students);
        TextView mailbody = (TextView) findViewById(R.id.emailtext);

        mailbody.setText("Here is the list for Tranport Tracking For Today: \n" + studlist);

        Button send=(Button) findViewById(R.id.emailsendbutton);

        send.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            Log.i("Send email", "");
                            String[] TO = {"h.trivedi04@gmail.com"};    
                            String[] CC = {"h.trivedi04@gmail.com", "harshit.trivedi22@gmail.com"};
                            Intent emailIntent = new Intent(Intent.ACTION_SEND);
                            emailIntent.setData(Uri.parse("mailto:"));
                            emailIntent.setType("text/plain");

                            emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
                            emailIntent.putExtra(Intent.EXTRA_CC, CC);
                            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Student Transport Track For Today");
                            emailIntent.putExtra(Intent.EXTRA_TEXT, studlist);

                            try {
                               startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                               finish();
                               Log.i("Finished sending email...", "");
                            } catch (android.content.ActivityNotFoundException ex) {
                               Toast.makeText(WebServiceActivity.this, 
                               "There is no email client installed.", Toast.LENGTH_SHORT).show();
                            }
                        }
                });
    }
}

And the main activity TransportActivity has:

Button webServiceButton = (Button)this.findViewById(R.id.webServiceButton);
        webServiceButton.setOnClickListener(new android.view.View.OnClickListener() 
        {
            public void onClick(View view) {
            Intent intent = new Intent( view.getContext(), WebServiceActivity.class);
            intent.putExtra("studlist", students.getText().toString());

            startActivity(intent);

        }
    });    

Here's the LogCat for the problem:

 05-01 21:37:31.267: E/AndroidRuntime(27758): FATAL EXCEPTION: main
 05-01 21:37:31.267: E/AndroidRuntime(27758): java.lang.RuntimeException: Unable to instantiate activity     ComponentInfo{com.harshit.studenttranstrack/com.harshit.studenttranstrack.WebServiceActivity}: java.lang.NullPointerException
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.app.ActivityThread.access$700(ActivityThread.java:165)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.os.Handler.dispatchMessage(Handler.java:99)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.os.Looper.loop(Looper.java:137)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at android.app.ActivityThread.main(ActivityThread.java:5455)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at java.lang.reflect.Method.invokeNative(Native Method)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at java.lang.reflect.Method.invoke(Method.java:525)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at dalvik.system.NativeStart.main(Native Method)
 05-01 21:37:31.267: E/AndroidRuntime(27758): Caused by: java.lang.NullPointerException
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at com.harshit.studenttranstrack.WebServiceActivity.<init>(WebServiceActivity.java:22)
 05-01 21:37:31.267: E/AndroidRuntime(27758):   at java.lang.Class.newInstanceImpl(Native Method)
有帮助吗?

解决方案 2

You can't getIntent() before onCreate() .There's simply no Intent available at that point. I believe the same is true for anything that requires a Context.

Set these in onCreate() method

String studlist = this.getIntent().getStringExtra("studlist");

其他提示

Try it like this inside onCreate() method.

Intent intent = this.getIntent(); //in the WebServiceActivity activity
String studlist = (String) intent .getStringExtra("studlist");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top