Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

java.lang.NullPointerException get url content

android java adt

Best Answer interpolacion, 05 June 2014 - 12:26 PM

package ir.shore.twofactor;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {


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

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		android.widget.EditText usernameTB;
		android.widget.TextView usernameLB;
		android.widget.EditText passwordTB;
		android.widget.TextView passwordLB;

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);

			usernameTB = (android.widget.EditText) rootView
					.findViewById(R.id.usernameTB);
			usernameLB = (android.widget.TextView) rootView
					.findViewById(R.id.usernameLB);
			passwordTB = (android.widget.EditText) rootView
					.findViewById(R.id.passwordTB);
			passwordLB = (android.widget.TextView) rootView
					.findViewById(R.id.passwordLB);

			android.widget.Button button = (android.widget.Button) rootView.findViewById(R.id.loginButton);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					String username = usernameTB.getText().toString();
					String password = usernameTB.getText().toString();
					String url = "http://shore.ir/actions.php?act=login&username=hamedupdate@yahoo.com&password=XXXXX";
					url = "http://shore.ir/actions.php?act=login&username="
							+ username + "&password=" + password;
					android.widget.Toast.makeText(getActivity(), url,
							android.widget.Toast.LENGTH_SHORT).show();
					/*
					 * AsyncHttpClient client = new AsyncHttpClient();
					 * client.get(url, new AsyncHttpResponseHandler() {
					 * 
					 * @Override public void onSuccess(String response) {
					 * android.widget.Toast.makeText(MainActivity.this,
					 * response, android.widget.Toast.LENGTH_SHORT).show(); }
					 * });
					 */
					android.widget.Toast.makeText(getActivity(),
							"Check Mobile", android.widget.Toast.LENGTH_SHORT)
							.show();

				}

			});

			return rootView;
		}
	}

}

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 UGP

UGP

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 05 June 2014 - 01:33 AM

Hello,

I want to get content of URL then I code:


package ir.shore.twofactor;


import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import com.loopj.android.http.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;


public class MainActivity extends ActionBarActivity {

	android.widget.EditText usernameTB;
	android.widget.TextView usernameLB;
	android.widget.EditText passwordTB;
	android.widget.TextView passwordLB;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        usernameTB = (android.widget.EditText)findViewById(R.id.usernameTB);
        usernameLB = (android.widget.EditText)findViewById(R.id.usernameLB);
        passwordTB = (android.widget.EditText)findViewById(R.id.passwordTB);
        passwordLB = (android.widget.EditText)findViewById(R.id.passwordLB);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }
    


    @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);
        android.widget.Button button = (android.widget.Button) findViewById(R.id.loginButton);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				String username = usernameTB.getText().toString();
				String password = usernameTB.getText().toString(); 
				String url = "http://shore.ir/actions.php?act=login&username=hamedupdate@yahoo.com&password=XXXXX";
				url =  "http://shore.ir/actions.php?act=login&username="+username+"&password="+password;
				android.widget.Toast.makeText(MainActivity.this, url,	android.widget.Toast.LENGTH_SHORT).show();
				/*AsyncHttpClient client = new AsyncHttpClient();
				client.get(url, new AsyncHttpResponseHandler() {
				    @Override
				    public void onSuccess(String response) {
				    	android.widget.Toast.makeText(MainActivity.this, response,	android.widget.Toast.LENGTH_SHORT).show();
				    }
				});*/
				android.widget.Toast.makeText(MainActivity.this, "Check Mobile",	android.widget.Toast.LENGTH_SHORT).show();
				
				
				
				
			}

		});
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

And now I get:

06-05 04:21:48.130: D/dalvikvm(791): Not late-enabling CheckJNI (already on)
06-05 04:21:53.970: I/Choreographer(791): Skipped 40 frames!  The application may be doing too much work on its main thread.
06-05 04:21:54.740: I/Choreographer(791): Skipped 53 frames!  The application may be doing too much work on its main thread.
06-05 04:21:55.230: D/gralloc_goldfish(791): Emulator without GPU emulation detected.
06-05 04:21:57.400: I/Choreographer(791): Skipped 50 frames!  The application may be doing too much work on its main thread.
06-05 04:21:59.110: D/dalvikvm(791): GC_FOR_ALLOC freed 198K, 9% free 3014K/3312K, paused 52ms, total 59ms
06-05 04:21:59.160: D/AndroidRuntime(791): Shutting down VM
06-05 04:21:59.160: W/dalvikvm(791): threadid=1: thread exiting with uncaught exception (group=0xb2ad0ba8)
06-05 04:21:59.170: E/AndroidRuntime(791): FATAL EXCEPTION: main
06-05 04:21:59.170: E/AndroidRuntime(791): Process: ir.shore.twofactor, PID: 791
06-05 04:21:59.170: E/AndroidRuntime(791): java.lang.NullPointerException
06-05 04:21:59.170: E/AndroidRuntime(791):     at ir.shore.twofactor.MainActivity$1.onClick(MainActivity.java:51)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.view.View.performClick(View.java:4438)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.view.View$PerformClick.run(View.java:18422)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.os.Handler.handleCallback(Handler.java:733)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.os.Looper.loop(Looper.java:136)
06-05 04:21:59.170: E/AndroidRuntime(791):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-05 04:21:59.170: E/AndroidRuntime(791):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 04:21:59.170: E/AndroidRuntime(791):     at java.lang.reflect.Method.invoke(Method.java:515)
06-05 04:21:59.170: E/AndroidRuntime(791):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-05 04:21:59.170: E/AndroidRuntime(791):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-05 04:21:59.170: E/AndroidRuntime(791):     at dalvik.system.NativeStart.main(Native Method)

 

What is my problem how can i fix it.



#2 interpolacion

interpolacion

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 June 2014 - 12:26 PM   Best Answer

package ir.shore.twofactor;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {


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

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		android.widget.EditText usernameTB;
		android.widget.TextView usernameLB;
		android.widget.EditText passwordTB;
		android.widget.TextView passwordLB;

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);

			usernameTB = (android.widget.EditText) rootView
					.findViewById(R.id.usernameTB);
			usernameLB = (android.widget.TextView) rootView
					.findViewById(R.id.usernameLB);
			passwordTB = (android.widget.EditText) rootView
					.findViewById(R.id.passwordTB);
			passwordLB = (android.widget.TextView) rootView
					.findViewById(R.id.passwordLB);

			android.widget.Button button = (android.widget.Button) rootView.findViewById(R.id.loginButton);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					String username = usernameTB.getText().toString();
					String password = usernameTB.getText().toString();
					String url = "http://shore.ir/actions.php?act=login&username=hamedupdate@yahoo.com&password=XXXXX";
					url = "http://shore.ir/actions.php?act=login&username="
							+ username + "&password=" + password;
					android.widget.Toast.makeText(getActivity(), url,
							android.widget.Toast.LENGTH_SHORT).show();
					/*
					 * AsyncHttpClient client = new AsyncHttpClient();
					 * client.get(url, new AsyncHttpResponseHandler() {
					 * 
					 * @Override public void onSuccess(String response) {
					 * android.widget.Toast.makeText(MainActivity.this,
					 * response, android.widget.Toast.LENGTH_SHORT).show(); }
					 * });
					 */
					android.widget.Toast.makeText(getActivity(),
							"Check Mobile", android.widget.Toast.LENGTH_SHORT)
							.show();

				}

			});

			return rootView;
		}
	}

}


Edited by interpolacion, 05 June 2014 - 12:27 PM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download