null pointer on floating button - android

I created floating action button for phone dial. tried to run the app but ended up error in my fab call.
I don't know what went wrong with floating action button.
can you show me how to fix it?
here it is my fab code
FloatingActionButton fabshare = (FloatingActionButton) findViewById(fab);
FloatingActionButton fabcall = (FloatingActionButton) findViewById(fab);
fabshare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
String shareSubject = String.valueOf(dataLaundry.getNama());
String shareBody = String.valueOf(dataLaundry.getAlamat());
i.putExtra(Intent.EXTRA_SUBJECT, "Laundrize - " + shareSubject);
i.putExtra(Intent.EXTRA_TEXT, shareSubject + "\n" + shareBody + "\n\n" +
"http://maps.google.com/?q="
+ alamat.getText().toString().toLowerCase().replaceAll(" ","+"));
startActivity(Intent.createChooser(i, "Share via"));
}
});
fabcall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent callIntent = new Intent (Intent.ACTION_DIAL, Uri.parse("tel:"+ noTelp));
startActivity(callIntent);
}
});\
and here the error
FATAL EXCEPTION: main
Process: com.example.rizkafs.laundrize, PID: 25885
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rizkafs.laundrize/com.example.rizkafs.laundrize.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2660)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730)
at android.app.ActivityThread.access$800(ActivityThread.java:185)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5847)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.rizkafs.laundrize.DetailsActivity.onCreate(DetailsActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6117)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730) 
at android.app.ActivityThread.access$800(ActivityThread.java:185) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5847) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 

Define id of both Fab icons in activity.
FloatingActionButton fabshare = (FloatingActionButton) findViewById(R.id.fabshare);
FloatingActionButton fabcall = (FloatingActionButton) findViewById(R.id.fabcall);

Related

Blank Screen showing up after Image Selected from Gallery

I am trying to get an image into the ImageView but whenever I click a picture on the Gallery the app shows a blank screen and closes down.
This ImageView is in a fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_auction_add, container, false);
auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageSelectAction(v);
}
});
dateTimeAction(v);
return v;
}
private void imageSelectAction(View v) {
auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
auction_add_image = v.findViewById(R.id.auction_add_imageView);
choosePicture();
}
private void choosePicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
if (data != null && data.getData() != null) {
Uri selectedImageUri = data.getData();
auction_add_image.setImageURI(selectedImageUri);
}
}
}
}
The Inital Declaration
public class auction_add extends Fragment {
private static final int SELECT_PICTURE = 10;
private static final int PERMISSION_CODE = 11;
ImageButton auction_add_imageViewButton;
//References to all Auction Add Page EditText
EditText auction_add_itemNameTxt;
EditText auction_add_descriptionTxt;
EditText auction_add_initialPriceTxt;
EditText auction_add_startTimeTxt;
EditText auction_add_endTimeTxt;
//Reference to ImageView
private ImageView auction_add_image;
final Calendar myCalendar = Calendar.getInstance();
Would Appreciate the help thanks!
Edit:
Followed is the error on the Run tab of Android Studio
W/System: A resource failed to call close.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.projectcrest, PID: 5618
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=242868079, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:31 flg=0x1 }} to activity {com.example.projectcrest/com.example.projectcrest.pages.LandingPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at com.example.projectcrest.fragments.auction_add.onActivityResult(auction_add.java:155)
at androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2905)
at androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2885)
at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:377)
at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:336)
at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:624)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:8310)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 5618 SIG: 9
By looking into the exception it seems that the App is crashing in #onActivityResult function at line auction_add_image.setImageURI(selectedImageUri); due to NPE (Null Pointer Exception). This expection/crash is caused since the auction_add_image object has null value instead of an ImageView.
After taking an close look into your code, I think the issue has occured due to following code:
auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageSelectAction(v);
}
});
Note that in #onClick function you are passing view v on which you are going to perform the #findViewById operation. Here the passed view v refers to the auction_add_imageViewButton instead of the v you have captured above.
I would suggest to modify the code as below (just rename the v variable in #onClick function):
auction_add_imageViewButton = v.findViewById(R.id.auction_add_imageView_button);
auction_add_imageViewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View buttonView) {
imageSelectAction(v);
}
});
This way the variable name collision could be avoided and the error would be resolved.
Also, in case this does not solve your problem, then please check whether the #findViewById is returning an ImageView instead of null. In case the Id provided to #findViewById is not present on the screen or in the child views of view (on which the #findViewById) is being performed then the #findViewById will return null value.
Also, check whether you have assigned null value to auction_add_image in anywhere your code.

Nullpointer on getIntent().getExtras();

code in RegisterActivity1:
Bundle extras = new Bundle();
...
case R.id.continue_button:
extras.putString("email",eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
code in RegisterActivity2:
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
I'm trying to pass email and password from activity 1 to activity 2, but nothing I have tried so far seems to be working. I always get a Nullpointer Exception here:
Bundle extras = getIntent().getExtras();
Any tips on how to fix this?
Here is the full method from Activity1, in case it has something to do with the override method...
#Override
public void onClick(View view) {
String email = eMail_eingabe.getText().toString().trim();
String password = passwort_1_eingabe.getText().toString().trim();
if (eMail_eingabe.getText().toString().isEmpty()) {
//eMail_eingabe.setError("Bitte email eingeben");
eMail_eingabe.setText("Bitte email eingeben");
eMail_eingabe.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
eMail_eingabe.setError("Bitte eine gültige E-Mail eingeben");
eMail_eingabe.requestFocus();
return;
}
if (password.length() < 6) {
passwort_1_eingabe.setError("Bitte mindestens 6 Zeichen eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_1_eingabe.getText().toString().isEmpty()) {
passwort_1_eingabe.setError("Bitte passwort eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_2_eingabe.getText().toString().isEmpty()) {
passwort_2_eingabe.setError("Bitte passwort eingeben");
passwort_2_eingabe.requestFocus();
return;
}
if (!passwort_1_eingabe.getText().toString().equalsIgnoreCase(passwort_2_eingabe.getText().toString())) {
passwort_2_eingabe.setError("Passwort stimmt nicht überein");
passwort_2_eingabe.requestFocus();
return;
}
switch (view.getId()) {
case R.id.continue_button:
extras.putString("email", eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
}
}
Exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hoimi, PID: 7812
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hoimi/com.example.hoimi.student.Register2_Student_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.hoimi.student.Register2_Student_Activity.<init>(Register2_Student_Activity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
at android.os.Handler.dispatchMessage(Handler.java:112) 
at android.os.Looper.loop(Looper.java:216) 
at android.app.ActivityThread.main(ActivityThread.java:7625) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
Bundle extras = getIntent().getExtras();
had to be called in the onCreate method, I called it before so it didn't work.
Someone gave the right answer, but deleted his post later (because he got 2 downvotes??)
Anyways, thanks alot.
Problem is not because of getExtras() its because of getIntent(). Check your getIntent() with null and use further.
Your code should be something like
if(getIntent() != null) {
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
}
Good luck.
when you call getIntent(), it returns the intent that you created in previous Activity.
public Intent getIntent ()
Return the intent that started this activity.
Activity documentation
so in second activity, your intent is null
in onCreate initiate your intent

Getting NullPointerException in a BankList Activity when sending data from MainActivity using intent in android [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to pass an arraylist between two activities, but my app crashes at the second activity. Can someone help me solve in this problem...
Here,
I have two parts of my MainActivity from where sending arraylist to another Activity which is BankList Activity
Part 1:
Intent intent = new Intent(MainActivity.this, BankList.class);
intent.putStringArrayListExtra("BANKLIST",bankListArrayList);
startActivity(intent);
Part 2:
Result<ArrayList<Bank>> banklist = (Result<ArrayList<Bank>>)data;
if(banklist.getCode().equals("00")){
Toast.makeText(this,"list Banks success",Toast.LENGTH_SHORT).show();
bankListArrayList = new ArrayList<>();
for(Bank bank :banklist.getData()){
bank.getIin();
bank.getLogo();
bank.getName();
b
ankListArrayList.add(bank.toString());
}
Log.d("BANK_ARRAYLIST","BANK_ARRAYLIST"+bankListArrayList);
Toast.makeText(this,"BANK_ARRAYLIST"+bankListArrayList,Toast.LENGTH_SHORT).show();
}
My Second Activity (BankList)
public class BankList extends AppCompatActivity {
ListView bankList;
ArrayList<String> bankdataList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_list);
bankList = (ListView)findViewById(R.id.bankList);
Intent intent = getIntent();
intent.getStringArrayListExtra("BANKLIST");
bankdataList.add(intent.toString());
Toast.makeText(BankList.this,"bank list"+bankdataList,Toast.LENGTH_SHORT).show();
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, bankdataList);
bankList.setAdapter(itemsAdapter);
}
}
And my Log is :-
11-09 12:58:01.548 1941-1941/com.example.rajdeeps.upi_integration E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rajdeeps.upi_integration, PID: 1941
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajdeeps.upi_integration/com.example.rajdeeps.upi_integration.BankList}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.rajdeeps.upi_integration.BankList.onCreate(BankList.java:30)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123)
You forgot to initialize your bankdataList Arraylist just initialize it
ArrayList<String> bankdataList= new ArrayList();;
Sample code
public class BankList extends AppCompatActivity {
ListView bankList;
ArrayList<String> bankdataList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_list);
bankdataList= new ArrayList();
bankList = (ListView)findViewById(R.id.bankList);
Intent intent = getIntent();
intent.getStringArrayListExtra("BANKLIST");
bankdataList.add(intent.toString());
Toast.makeText(BankList.this,"bank list"+bankdataList,Toast.LENGTH_SHORT).show();
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, bankdataList);
bankList.setAdapter(itemsAdapter);
}
}
You have not initialize bankdataList before add items.
bankdataList = new ArrayList<String>();
bankdataList.add(intent.getStringArrayListExtra("BANKLIST"));

android.content.Context.getPackageName()' on a null object reference

I am begineer in android and having problems
Above error showing while i'm trying to open main activity from login button on login activity.
when pressing the login button suddenly app stops and following error is showing
following error is showing in android monitor
10-20 18:47:23.314 13432-13432/com.antherx.prasenjit.ihdfbachatgat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antherx.prasenjit.ihdfbachatgat, PID: 13432
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.antherx.prasenjit.ihdfbachatgat/com.antherx.prasenjit.ihdfbachatgat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4358)
at com.antherx.prasenjit.ihdfbachatgat.MainActivity.<init>(MainActivity.java:26)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
this is login activity code is their any problem in this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Please do not
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
String wel_on_login = "Welcome On IHDF Bachat Gat app";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_login,Toast.LENGTH_LONG);
welcome.show();
final Button signIn = (Button)findViewById(R.id.email_sign_in_button);
final Button register = (Button)findViewById(R.id.registration) ;
final Intent main_trans = new Intent(this, MainActivity.class);
final Intent reg_trans = new Intent(this, register.class);
signIn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(main_trans);
}
});
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(reg_trans);
}
});
this is main activity codes is their any problem in here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.home_toolbar);
setSupportActionBar(toolbar);
newMember = (MenuItem)findViewById(R.id.new_member);
newProfile = (MenuItem)findViewById(R.id.profile) ;
Drawer =(DrawerLayout)findViewById(R.id.drawerLayout);
sToggle = new ActionBarDrawerToggle(this,Drawer,R.string.open,R.string.close);
Grp_ico = (ImageView)findViewById(R.id.grp_ico);
Drawer.addDrawerListener(sToggle);
sToggle.syncState();
getSupportActionBar();
String wel_on_main = "You are successfully on Main Home";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
welcome.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_member :
startActivity(new_new_member);
case R.id.profile :
startActivity(new_new_profile);
case R.id.setting :
startActivity(new_new_setting);
case R.id.meeting :
startActivity(new_new_meeting);
default:
return super.onOptionsItemSelected(item);
}
}
for me i was trying to use the application with no base context accidently.
so i had in my application override:
#Override
protected void attachBaseContext(Context newBase) {
}
//instead of calling its super:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);}
Change your getApplicationContext() from
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
to either getBaseContext() or preferably this

Set an image on imageview using arraylist strings and strings

Brief Description: This is a speech to text app, if the word they spoken is also a word in the database file then it will also have an image of that word spoken.
So I attempted to use imageResource to set the image but it failed, as it is using an ArrayList and a String for the first part of the imageResoruce function. which is assumed to be causing the error message as it crashes when i open the application.
Main.java
public class Main extends Activity {
private static final int VR_Request = 100;
private final String pathname = ".png"; //path name of an image file stored in the drawable folder
TextView speechInput;
TextView matchOrNot;
String[] wordBank; //
ArrayList<String> wordBANK;
ImageButton speechBtn;
ImageView image;
Resources res = getResources();
int resID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reverse_pictionary);
speechInput = (TextView) findViewById(R.id.english_word);
matchOrNot = (TextView) findViewById(R.id.matchOrNot);
wordBank = getResources().getStringArray(R.array.Words);
speechBtn = (ImageButton) findViewById(R.id.mic_pic_button);
wordBANK = new ArrayList<String>(Arrays.asList(wordBank));
image = (ImageView) findViewById(R.id.imageOfword);
}
public void onMic(View view) {
promptSpeechInput();
}
public void promptSpeechInput() {
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == VR_Request && resultCode == RESULT_OK) {
ArrayList<String> result = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(wordBANK.contains(result.get(0).toLowerCase())){
speechInput.setText(result.get(0).toUpperCase());
matchOrNot.setText("MATCH");
resID = res.getIdentifier(result.get(0).toLowerCase()+pathname, "drawable", getPackageName());
image.setImageResource(resID);
}else {
speechInput.setText(result.get(0));
matchOrNot.setText("NO MATCH");
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
}
RunTime Error Message:
08-10 21:07:37.678 2344-2344/com.example.speechtotext E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.speechtotext, PID: 2344
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.speechtotext/com.example.speechtotext.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at com.example.speechtotext.Main.<init>(Main.java:38)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Any Ideas? Thank you in advance!
Move the code :
Resources res = getResources();
into the onCreate() method.
You can not use getResources() before the activity created.

Categories

Resources