I am working on android GCM to display some notification. It works fine. But when I add a code that could enable me to start the Home activity upon clicking the notification, it crashes. If I remove the PendingIntent It works fine but with no action.
Below is the code I am using
public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class);
int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId
int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags);
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
createNotification(from, message);
}
// Creates notification based on title and body received
private void createNotification(String title, String body) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pIntent);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
and below is the error log I am getting.
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: FATAL EXCEPTION: main
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: Process: abenakebe.yournet.com.abenakebe, PID: 14201
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3623)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.access$2000(ActivityThread.java:198)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:101)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.PendingIntent.getActivity(PendingIntent.java:286)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.PendingIntent.getActivity(PendingIntent.java:252)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler.<init>(GcmMessageHandler.java:25)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1684)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3620)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.access$2000(ActivityThread.java:198)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
You should move this two things at least to onCreate() or onMessageReceived()
Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags);
It crashes because this doesn't have Context before onCreate was called.
SHould be like this:
public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
Intent intent;
int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId
int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one
PendingIntent pIntent;
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
intent = new Intent(GcmMessageHandler.this, HomeActivity.class);
pIntent = PendingIntent.getActivity(this, requestID, intent, flags);
createNotification(from, message);
}
// Creates notification based on title and body received
private void createNotification(String title, String body) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pIntent);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
Related
Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));
I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);
How can I start activity in adapter? I use this method in my adapter:
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context,asabani_cat.class));
}
});
But this worked just for fragment of my app, when I called this code in Activity App suddenly crashed!
Another question that I have, is it the good way to start activity in adapter?
my logcat:
11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: FATAL EXCEPTION: main 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: java.lang.NullPointerException 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.katibehpayam.mahdi.katibehpayam.adapter_common$7.onClick(adapter_common.java:266) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View.performClick(View.java:4377) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18044) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 11-27 21:25:25.086 22237-22237/com.katibehpayam.mahdi.katibehpayam E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
You could try:
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(),asabani_cat.class));
}
});
You should post the error from the Logcat so we can help you further. However from what you've shown I can find nothing inherently wrong with your code. Make sure your context is set correctly. Furthermore if you are already in an Activity you do not need to say
context.startActivity(new Intent(context,asabani_cat.class));
you can simply say
startActivity(new Intent(this, asabani_cat.class));
If you are not in an Activity then you need to pass the context like so
MyAdapter myAdapter = new MyAdapter(this);
Then inside your adapter set your context like this:
MyAdapter(Context context) {
this.context = context;
}
EDIT
Your logcat shows a null pointer exception is the cause of your crash. Can you please post the code from adapter_common.java at line 266? Thanks
In kotlin, on your view:
class YourClass: ArrayAdapter<Feature>(), View.OnClickListener{
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var yourView = inflater.inflate(mResource, parent, false)
yourView .setOnClickListener(){
onClick(convertView)
}
}
override fun onClick(v: View?) {
val intent = Intent(mContext, SeckondActivity::class.java)
mContext.startActivity(intent)
}
}
Every time I click on the button, the app crashes. I have trouble finding the bug, but I'm sure it has to do something with either the onClickListener, or the onClick function.
Note: I'm using Parse API for some back end stuff. But I highly doubt it has anything to do with any of the Parse stuff.
Here's what my code looks like:
public class SignUpActivity extends Activity {
protected EditText mUserName;
protected EditText mPassword;
protected EditText mEmail;
protected Button mSignUpButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_sign_up);
mUserName = (EditText) findViewById(R.id.usernameField);
mPassword = (EditText) findViewById(R.id.passwordField);
mEmail = (EditText) findViewById(R.id.emailField);
mSignUpButton = (Button) findViewById(R.id.signupButton);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = mUserName.getText().toString();
String password = mPassword.getText().toString();
String email = mEmail.getText().toString();
username = username.trim();
password = password.trim();
email = email.trim();
if(username.isEmpty() || password.isEmpty() || email.isEmpty()){
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else{
setProgressBarIndeterminateVisibility(true);
ParseUser newUser = new ParseUser();
newUser.setUsername(username);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if(e == null){
// success
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_up, menu);
return true;
}
}
Here's my Log Cat:
04-14 14:19:05.881: E/AndroidRuntime(1156): FATAL EXCEPTION: main 04-14 14:19:05.881:
E/AndroidRuntime(1156): Process: com.ebad.ribbit, PID: 1156 04-14 14:19:05.881:
E/AndroidRuntime(1156): java.lang.NullPointerException 04-14 14:19:05.881:
E/AndroidRuntime(1156): at com.ebad.ribbit.SignUpActivity$1.onClick(SignUpActivity.java:40) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.view.View.performClick(View.java:4438) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.view.View$PerformClick.run(View.java:18422) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.os.Handler.handleCallback(Handler.java:733) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.os.Handler.dispatchMessage(Handler.java:95) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.os.Looper.loop(Looper.java:136) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at java.lang.reflect.Method.invokeNative(Native Method) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at java.lang.reflect.Method.invoke(Method.java:515) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at dalvik.system.NativeStart.main(Native Method)
EDIT:
As requested, here is line 40:
String username = mUserName.getText().toString();
EDIT 2:
Here is the log cat after adding Log.i(TAG, mUserName == null ? "mUserName is null" : mUserName.getText() == null ? "mUserName.getText() is null" : "nothing is null"); :
04-14 15:21:47.955: E/AndroidRuntime(1318): FATAL EXCEPTION: main
04-14 15:21:47.955: E/AndroidRuntime(1318): Process: com.ebad.ribbit, PID: 1318
04-14 15:21:47.955: E/AndroidRuntime(1318): java.lang.NullPointerException
04-14 15:21:47.955: E/AndroidRuntime(1318): at com.ebad.ribbit.SignUpActivity$1.onClick(SignUpActivity.java:43)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.view.View.performClick(View.java:4438)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.view.View$PerformClick.run(View.java:18422)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.os.Handler.handleCallback(Handler.java:733)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.os.Looper.loop(Looper.java:136)
04-14 15:21:47.955: E/AndroidRuntime(1318): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 15:21:47.955: E/AndroidRuntime(1318): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 15:21:47.955: E/AndroidRuntime(1318): at java.lang.reflect.Method.invoke(Method.java:515)
04-14 15:21:47.955: E/AndroidRuntime(1318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 15:21:47.955: E/AndroidRuntime(1318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 15:21:47.955: E/AndroidRuntime(1318): at dalvik.system.NativeStart.main(Native Method)
EDIT 3:
Upon Request, here is my activity_sign_up.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SignUpActivity" >
<EditText
android:id="#+id/userNameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="#string/username_hint"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/userNameField"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword" />
<EditText
android:id="#+id/emailField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/passwordField"
android:ems="10"
android:hint="#string/email_hint"
android:inputType="textEmailAddress" />
<Button
android:id="#+id/signupButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/emailField"
android:layout_below="#+id/emailField"
android:layout_marginTop="50dp"
android:text="#string/sign_up_button_label" />
</RelativeLayout>
Do this please:
#Override
public void onClick(View v) {
if(mUserName != null && mPassword != null && mEmail != null && mUserName.getText() != null && mPassword.getText() != null && mEmail.getText() != null)
{
Your original code here.
}
}
Update:
Typo
mUserName = (EditText) findViewById(R.id.usernameField); should be userNameField?
<EditText android:id="#+id/userNameField" ...
If mUsername is null, calling getText() on it will cause a NullPointerException. You should check that it's not null before doing this. Try changing to the following:
if(mUserName == null || mPassword == null || mEmail == null){
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
String username = mUserName.getText().toString();
String password = mPassword.getText().toString();
String email = mEmail.getText().toString();
username = username.trim();
password = password.trim();
email = email.trim();
Try
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
https://developer.android.com/guide/topics/ui/dialogs.html
Also make sure that SignUpActivity is declared in the app manifest. I guess make sure that all activities are set up in the manifest.
EDIT 1
If line 40 is the issue, then
mUserName is null
or
mUserName.getText() is null. So create a couple checks before hand and also add some debug logs to just verify that they are not null.
Make sure that R.layout.activity_sign_up has usernameField as an id. Also make sure in any other app/res/layout folders (ie layout-v14), that all R.layout.activity_sign_up has the edittext as well.
If this errors occurred in your line 40, that means your are getting null from your EditText.
Try it in every place you are getting a string. At least it will save you from getting null pointer exception from EditText.
String username = String.valueOf(mUserName.getText());
String password = String.valueOf(mPassword.getText());
String email = String.valueOf(mEmail.getText());
I am trying to create an application that sends a file to a server. The application consists of one button that when clicked, will upload a file to a server. Here is the code that I have so far:
public class MainActivity extends Activity implements OnClickListener {
final String TAG = "sendButton";
final String TAG2 = "messageButton";
TextView messageText;
Button uploadButton;
int serverResponseCode = 0;
String result = null;
String url = "http://192.168.1.18";
File file = new File("example.txt");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById(R.id.button2).setOnClickListener(this);
//findViewById(R.id.button2).setOnClickListener(this);
setupsendMessage();
//setupmessageButton();
uploadButton = (Button)findViewById(R.id.button1);
messageText = (TextView)findViewById(R.id.button2);
}
private void setupsendMessage() {
// do something when the button is pressed
//
Button sendButton = (Button) findViewById(R.id.button1);
sendButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file);
builder.addPart("file", fileBody);
final HttpEntity yourEntity = builder.build();
post.setEntity(yourEntity);
HttpResponse response = client.execute(post);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
Log.v("result", result);
}
catch(Exception e)
{
e.printStackTrace();}
Log.i(TAG, "File Sent to Server");
Toast.makeText(MainActivity.this, "File Sent to Server", Toast.LENGTH_LONG).show();
}
});
};
When I run the code the application everything appears fine, but when I press the button nothing happens and I get an error on the trace.
The error is as follows:
04-14 17:20:39.504: W/IInputConnectionWrapper(1693): showStatusIcon on inactive InputConnection
04-14 17:20:46.121: W/System.err(1693): java.net.SocketException: Permission denied
04-14 17:20:46.121: W/System.err(1693): at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
04-14 17:20:46.121: W/System.err(1693): at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
04-14 17:20:46.121: W/System.err(1693): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
04-14 17:20:46.121: W/System.err(1693): at java.net.Socket.checkOpenAndCreate(Socket.java:802)
04-14 17:20:46.121: W/System.err(1693): at java.net.Socket.connect(Socket.java:948)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-14 17:20:46.121: W/System.err(1693): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-14 17:20:46.121: W/System.err(1693): at school.project.application.MainActivity$1.onClick(MainActivity.java:117)
04-14 17:20:46.121: W/System.err(1693): at android.view.View.performClick(View.java:2485)
04-14 17:20:46.131: W/System.err(1693): at android.view.View$PerformClick.run(View.java:9081)
04-14 17:20:46.131: W/System.err(1693): at android.os.Handler.handleCallback(Handler.java:587)
04-14 17:20:46.131: W/System.err(1693): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 17:20:46.131: W/System.err(1693): at android.os.Looper.loop(Looper.java:130)
04-14 17:20:46.131: W/System.err(1693): at android.app.ActivityThread.main(ActivityThread.java:3696)
04-14 17:20:46.131: W/System.err(1693): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:20:46.131: W/System.err(1693): at java.lang.reflect.Method.invoke(Method.java:507)
04-14 17:20:46.131: W/System.err(1693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-14 17:20:46.131: W/System.err(1693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-14 17:20:46.131: W/System.err(1693): at dalvik.system.NativeStart.main(Native Method)
04-14 17:20:46.131: I/sendButton(1693): File Sent to Server
You may need to enable Internet access in the project's manifest file:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
You need to have permission to send data via internet, Add the following line to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />