bottom bar button crash [duplicate] - java

This question already has answers here:
Could not instantiate activity - android studio
(3 answers)
Closed 1 year ago.
I have been trying to figure out why suddenly my bottom bar navigation button for a particular activity causes crashes.
On MainActivity, I have the following bottom bar nav:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
//mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_events:
//mTextMessage.setText(R.string.title_events);
return true;
case R.id.navigation_offpeak:
//mTextMessage.setText(R.string.title_offpeak);
return true;
case R.id.navigation_deals:
Intent reservationsIntent = new Intent(MainActivity.this, DealsHomeActivity.class);
startActivity(reservationsIntent);
//mTextMessage.setText(R.string.title_bookings);
return true;
case R.id.navigation_me:
Intent memberProfileIntent = new Intent(MainActivity.this, MemberProfileActivity.class);
startActivity(memberProfileIntent);
//logout();
//mTextMessage.setText(R.string.title_me);
return true;
}
return false;
}
};
Then, my activity is instantiated like all my other activities, like the MemberProfileActivity.class.
However pushing the button to get to MemberProfileActivity.class works as expected on the phone, but when doing this for DealsHomeActivity.class results in a crash with the following logcat:
05-27 15:38:40.473 31978-31978/asia.diningcity.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: asia.diningcity.android, PID: 31978
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{asia.diningcity.android/asia.diningcity.android.activities.DealsHomeActivity}: java.lang.InstantiationException: java.lang.Class cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.InstantiationException: java.lang.Class cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
I have been looking at the two activities in Java to see if there are any differences in the onCreate method to start the activity but none.
So I don't understand where this could be coming from.
Please note also this was working fine before, and since then I haven't touched the menu bar or that Deals activity so I don't know what could have caused this.

i believe your class is either an abstract class or a private class, abstract or private classes can not be instantiated,
Abstract classes are by definition not instantiable.
if your class is abstract or private, i assume it's something like this
private abstract class DealsHomeActivity extends AppCompatActivity
change to
public class DealsHomeActivity extends AppCompatActivity

Related

Hi! I am new to android app development. I tried to create a simple counter but after build getting error "Unable to instantiate activity"

public abstract class MainActivity extends AppCompatActivity {
Button decrement;
Button increment;
TextView counter_view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Log.i("tag", "onCreate: Created Successfully");
increment=findViewById(R.id.inc_btn);
decrement=findViewById(R.id.dec_btn);
counter_view=findViewById(R.id.counter);
increment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String cnt_text=counter_view.getText().toString();
int cnt_no= Integer.parseInt(cnt_text);
cnt_no=cnt_no+1;
counter_view.setText(cnt_no+"");
}
});
decrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String cnt_text=counter_view.getText().toString();
int cnt_no=Integer.parseInt(cnt_text);
cnt_no=cnt_no-1;
counter_view.setText(cnt_no+"");
}
});
}
Error:
2021-02-10 00:29:06.870 16714-16714/? E/Zygote: v2
2021-02-10 00:29:06.871 16714-16714/? E/Zygote: accessInfo : 0
2021-02-10 00:29:07.017 16714-16714/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.counter_app, PID: 16714
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.counter_app/com.example.counter_app.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.counter_app.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2849)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.counter_app.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 
I suppose this is your first screen/activity/class that is displayed on launch i.e it is your launcher activity. If yes,
the launcher activity cannot be abstract. Because when an app is launched from the home screen on an Android device, the Android OS creates an instance of the activity in the application you have declared to be the launcher activity. And abstract classes can not be instantiated, they can only be sub-classed.
Please remove the word abstract before your class name.
add to your manifest file this line
<activity android:name="your.package.name.MainActivity"/>

Why is my app crashing when I try to open a Navigation Drawer Activity from an Empty Activity? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am a beginner in android My app starts with a Splash Screen which goes to an Empty Activity where it takes some info. It works fine till here. After that I used a submit button to guide the user to the Navigation drawer activity, but somehow the app crashes. I tried to replace the navigation drawer activity with an Empty activity on button click, which worked fine. I also tried launching the navigation drawer activity from splash screen, didn't work as well.
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(getApplicationContext(), ChoiceSelection.class);
startActivity(gotoChoiceSelection);
}
});
(ChoiceSelection is my Navigation drawer activity)
logcat
08-09 20:39:15.765 19793-19793/comviewappisome.google.sites.iamengineermu E/AndroidRuntime: FATAL EXCEPTION: main
Process: comviewappisome.google.sites.iamengineermu, PID: 19793
java.lang.RuntimeException: Unable to start activity ComponentInfo{comviewappisome.google.sites.iamengineermu/comviewappisome.google.sites.iamengineermu.ChoiceSelection}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6784)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference
at comviewappisome.google.sites.iamengineermu.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:101)
at android.app.Fragment.performCreateView(Fragment.java:2611)
at android.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1459)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1247)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1701)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3611)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6196)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:428)
at android.app.Activity.setContentView(Activity.java:2659)
at comviewappisome.google.sites.iamengineermu.ChoiceSelection.onCreate(ChoiceSelection.java:37)
at android.app.Activity.performCreate(Activity.java:6984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6784)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
Suggest me a solution for this.
if you are using a activity to Navigation drawer activity
you can try this in the place of getApplicationContext()
I think you should change your java code like this:
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
startActivity(gotoChoiceSelection );
}
});
Try this code, it worked for me:
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
startActivity(gotoChoiceSelection );
}
});
Just replace getApplicationContext() with this.

Error when accessing method from subclass

i`m new to android development and i wanted to make an audio recorder, when i want to access the method for starting of recording from my main activity it always gives an error. Below is my code. I hope you can help me:
This is the mainActivity:
public class MainActivity extends AppCompatActivity {
Boolean isRecording = false;
Record record;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
record = new Record();
}
public void recordAudio(View view){
if(!isRecording)
{
isRecording = true;
record.startRecording();
}
else{
isRecording = false;
record.stopRecording();
}
}
Below is the subclass:
public class Record extends MainActivity {
public void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, getBufferSize());
int i = recorder.getState();
if (i == 1)
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
#Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
buttonRecord.setText(R.string.button_stop_record);
}
Thanks for your help!
Here is the exact error code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com, PID: 31778
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6199) 
at android.widget.TextView.performClick(TextView.java:11090) 
at android.view.View$PerformClick.run(View.java:23647) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Caused by: java.lang.IllegalArgumentException: Invalid audio buffer size.
at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:751)
at android.media.AudioRecord.<init>(AudioRecord.java:385)
at android.media.AudioRecord.<init>(AudioRecord.java:289)
at com.Record.startRecording(Record.java:63)
at com.MainActivity.recordAudio(MainActivity.java:35)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:6199) 
at android.widget.TextView.performClick(TextView.java:11090) 
at android.view.View$PerformClick.run(View.java:23647) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
In Android Activity is usually stared by the user code but the object creation is not the responsibility of the user, rather it is the Android framework which does this.
Your case has Record extends MainActivity and MainActivity being an Activity makes Record also an Activity. So you need to start it either make it the launcher main activity in manifest file or use startActivity() (or startActivityForResult()).
The code record = new Record(); Here you create the instance of Record yourself that too in the parent class MainActivity. This is not a very good idea from both programming in Android and Java Object oriented point of view. (Hence you can but should not choose to do so)
Refer one answer from another post
https://stackoverflow.com/a/14956056/504133
I suggest you to make simple Android application with two or three activities, with each having simple UI layout. You can learn there and then apply the concepts for more complex application as AudioRecorder.
The error says com.Record.startRecording(Record.java:63) is the cause of: java.lang.IllegalArgumentException: Invalid audio buffer size.
Please call AudioRecord.getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) to make sure your buffer size is big enough before you instantiate a new AudioRecord. Just like it says in the documentation.
Please note that you should also check if your AudioRecord instance was initialized correctly by calling getState() (and checking that it returns STATE_INITIALIZED).

How to pass a variable to findViewById method using classes and instances? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I've been trying to make this tic-tac-toe game on android studios recently, I only have basic knowledge about android studio and java so i wanted to accomplish this app by making classes and objects, but whenever i try to run my app the app crashes i don't know what's wrong with the code it compiles correctly, can someone help me out?
this is the logcat window:
11-04 23:48:20.249 4534-4534/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-04 23:48:20.258 4534-4534/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-04 23:48:20.480 4534-4534/com.example.home.tictactoe I/InstantRun: starting instant run server: is main process
11-04 23:48:20.845 4534-4534/com.example.home.tictactoe D/AndroidRuntime: Shutting down VM
11-04 23:48:20.851 4534-4534/com.example.home.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.home.tictactoe, PID: 4534
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.tictactoe/com.example.home.tictactoe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at com.example.home.tictactoe.MainActivity$1pawn.hideObj(MainActivity.java:45)
at com.example.home.tictactoe.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
and this is my MainActivity.java file
package com.example.home.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public void clickButton(View view){
Log.i("test","working");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class pawn{
int resID = 0;
ImageView object2 = (ImageView) findViewById(resID);
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
final pawn x_11 = new pawn();
final pawn zero_11 = new pawn();
x_11.resID = R.id.x11;
zero_11.resID=R.id.zero11;
x_11.hideObj();
}
}
See picture:
ImageView object2 = (ImageView) findViewById(resID); is called when your pawn object created. At this point you've not set resId for your object yet so it will be 0. This cause object2 to null.
You can change your class like this:
class pawn {
int resID = 0;
ImageView object2;
public pawn(int resID) {
this.resID = resID;
object2 = (ImageView) findViewById(resID)
}
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
Then use it
pawn x_11 = new pawn(R.id.x11);
x_11.hideObj();

Getting Crash When trying to initiate an Activity

Almost all of the current navigation in the application is working perfectly fine and when doing the same things for other parts i navigate with.
I am getting this error when trying to navigate from one activity to another:
FATAL EXCEPTION: main
Process: com.package, PID: 15338
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package/ui.activity.AccountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
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 'int android.view.View.getId()' on a null object reference
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:181)
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:174)
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:160)
at ui.activity.AccountActivity.setInitialFragment(AccountActivity.java:38)
at ui.activity.BaseActivity.onCreate(BaseActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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) 
I can track where the Null reference is being made and have tried a few things to try and bypass it but they all crash the application.
Here is the block where i initiate the activity:
#Override
public void onMyAccountOptionSelected() {
Intent intent = new Intent(this, AccountActivity.class);
startActivity(intent);
}
Then it gets caught here on view.getId(), this one is in baseActivity:
private void setInitialFragment(View view, Fragment fragment) {
if (this.getCurrentFragment() == null) {
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(view.getId(), fragment).commit();
}
}
I am initially calling setInitialFragment() in AccountActivity which extends my baseActivity:
#Override
protected void setInitialFragment() {
fragment = (AccountFragment) AccountFragment.newInstance();
setInitialFragment(fragment);
}
As you can see above there is now an AccountFragment, in this class i extend from an abstractFragment and have a method to declare a newInstance:
public static Fragment newInstance() {
AccountFragment fragment = new AccountFragment();
return fragment;
}
I cant find anywhere else where it may be affecting the process used for navigating to this activity. I am hoping to find a method to make a quick fix or to find a more appropriate way to solve this.
Any help is much appreciated!
You didn't provide us with all the information needed to be really helpfull.
i'm imaging that your setInitialFragment in your AccountActivity would be something like this:
public class AccountActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.container);
AccountFragment fragment = AccountFragment.getInstance();
setInitialFragment(fragment);
}
}
Based on what you posted, your are calling 2 different methods, this:
setInitialFragment(Fragment fragment);
is not the same as this
setInitialFragment(View view, Fragment fragment)

Categories

Resources