Passing Arraylist from an interface implementattion to a class [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am a beginner, and I am trying to use ArrayList from an Interface. I got the structure from an article here. The problem is it's giving me null when I try to use it in a Fragment.
The class is:
public class NewsFRagment extends Fragment implements BackgroundTask.CallBack {
private ArrayList<NewsPOJO> arrayList1;
public NewsFRagment() {
// Required empty public constructor
}
#Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {
this.arrayList1 = arrayList;
Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_fragment, container, false);
BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
backgroundTask.execute();
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));
return v;
}
#Override
public String toString() {
return "News Fragment";
}
}
I need to use arraylist from the onSuccess and pass it in my setAdapter method, Problem is its null when I pass it in setAdaper, but it's not null in the onSuccess, How can I solve it?
This is logcat:
06-03 04:52:50.367 21851-21851/hilz.reycfrag W/art: Failed to find OatDexFile for DexFile /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex ( canonical path /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex) with checksum 0xd8bc1e5a in OatFile /data/data/hilz.reycfrag/cache/slice-slice_8-classes.dex
06-03 04:52:51.150 21851-21851/hilz.reycfrag D/AndroidRuntime: Shutting down VM
06-03 04:52:51.150 21851-21851/hilz.reycfrag E/AndroidRuntime: FATAL EXCEPTION: main
Process: hilz.reycfrag, PID: 21851
java.lang.RuntimeException: Unable to start activity ComponentInfo{hilz.reycfrag/hilz.reycfrag.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at hilz.reycfrag.NewsFRagment.onCreateView(NewsFRagment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-03 04:52:51.178 21851-21897/hilz.reycfrag D/arraylist in onSuccess: 4

#Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {
this.arrayList1 = arrayList;
Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
// Make sure your fragment is done view loading before on success.
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));
}
Call the below code on start, show a progress dialog, and cancel on success:
BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
backgroundTask.execute();

Check in what order the callbacks happen...
Your arraylist may not be initialized, when the callback happens at setAdapter as you only declare it. You never initialize it in constructor. As opposed to in the OnSuccess call back, where you receive an arraylist reference as input and set your local instance variable to it.
The creation of the view would be the initial callback run, so if you need to pass the list you need to initialise it first, you could for instance add an initialization to the constructor:
public NewsFRagment() {
// Required empty public constructor
arrayList1 = new ArrayList<NewsPOJO>();
}
Or you could have a specific initialise method, that you call after doing needed stuff in the view creation event first (if you need to pre-set the list).

Related

App crashes while trying to get my Camera IDs [duplicate]

This question already has answers here:
Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
(2 answers)
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
Closed 5 years ago.
So I'm relatively new to Android and trying to build a Camera App. But I'm allready stuck with catching the Camera IDs.
I have my public CameraManager, a public String array and a public TextView and I was just trying to get the CameraIDs from my CameraManager and display the first of my IDs in my textview. And the App builds without errors but as soon as I try to execute it on my phone, it just crashes. Where is my mistake?
public CameraManager mCamera;
public String[] cameraIDs;
public TextView text = (TextView)findViewById(R.id.text);
And in my onCreate function:
try {
cameraIDs = mCamera.getCameraIdlist();
text.setText(cameraIDs[0]);
} catch (CameraAccessExeption e) {
text.setText("Sorry, couldn't find camera device.");
}
And, as requested, the error log from my phone:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.ThrowActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.access$1000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.test.ThrowActivity.<init>(ThrowActivity.java:26)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
... 9 more
line ThrowActivity.java:26
public TextView text = (TextView)findViewById(R.id.text);
you need to initialise views inside the Activity onCreate method. Before setContentView is called you cannot look up views by id.
So replace this:
public TextView text = (TextView)findViewById(R.id.text);
with:
public TextView text;
//....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
text = (TextView)findViewById(R.id.text);
//...

Java / Android: Collect all Button methods in Helper class

Heyho.
Is it possible that I put all onClick() Methods in a single class to have a better Overview?
That's what I've tried:
public class ButtonHandler extends MainActivity
{
public ButtonHandler()
{
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnClick1(View v)
{
if (_inputTextView != null)
{
_inputTextView.append("1");
}
}
}
But this gives me the following Exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fayt.taschenrechner, PID: 4009
java.lang.IllegalStateException: Could not find method OnClick1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_1'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
Is that even possible or am I trying something impossible?
Greets
Your app crash is because you pass your view as parameter. Currently you are setting android:onClick which there is no parameter in android system which will cause IllegalStateException. Learn how to make onClick programatically then you could put all your event inside on class. You could simply create another java class inside your project for handle all the click event rather than extends a mainActivity .

New Activity crashes on startup [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
When clicking a Button to switch from the main Activity to my "ColoursGame" Activity the app crashes.
I'm still a beginner so not an expert at debugging.
The main activity code
Button colorsGame = (Button) findViewById(R.id.colours);
colorsGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ColoursGame.class));
}
});
Manifest new activity
<activity android:name=".ColoursGame"
android:label="ColourGame"
android:theme="#style/AppTheme.NoActionBar"></activity>
ColoursGame Activity OnCreate code
public class ColoursGame extends Activity {
int livesCount = 3;
String x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colours_game);
Button start = (Button) findViewById(R.id.startColors);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vSwitch.showNext();
x = String.valueOf(livesCount);
lives.setText(x);
text();
textColor();
backgroundColor();
start();
}
});
}
The Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aynaar.numbersgameforkids, PID: 3278
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aynaar.numbersgameforkids/com.aynaar.numbersgameforkids.ColoursGame}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2323)
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
Don't call findViewById outside of onCreate, there's no Window to call findViewById at that point.
If you still get a NullPointer, then you must have #+id/startColors in activity_colours_game.xml
Answer explanation here
Button start = (Button) findViewById(R.id.startColors);
According to the error you are trying to reach a button that doesnt exist in your activity layout file (whatever it is). Check the id of your button and make sure it is placed on the layout page that related with your "ColoursGame" Activity.

SharedPreferences returns null no matter what method I use

Beginner Java/Android developer here. I know this is an unbelievably over-asked question, but everything I've tried from every other thread I could find DOES NOT WORK. No matter what. Either it will return null, or not work at all and throw an error on the line meant for getting a SharedPreferences object. Apologies if this is just a really tiny error.
Full stack trace and code below.
05-03 20:35:38.225 18928-18928/com.example.admin2.clicker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin2.clicker, PID: 18928
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin2.clicker/com.example.admin2.clicker.Home}: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' 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 interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
at com.example.admin2.clicker.Home.<init>(Home.java:50)
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)
The code:
// Used to fetch SharedPreferences object
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
TextView xpText = (TextView) findViewById(R.id.xpText);
TextView lvText = (TextView) findViewById(R.id.lvText);
xpText.setText("Experience: 0/100");
lvText.setText("Level 1");
gameData = this.getSharedPreferences("gameData", MODE_PRIVATE);
}
// The line throwing the error + its context
{
//boolean firstRun = gameData.getBoolean("f", false);
boolean firstRun = true;
if (firstRun) {
SharedPreferences.Editor e = gameData.edit();
e.putInt("xp", 0);
e.putInt("mxp", 100);
e.putInt("lv", 1);
e.putInt("$", 0);
e.putInt("$c", 0);
e.apply();
}
}
If you have any questions or need more info, let me know. I'm not sure of everything I need to provide. Thanks.
You put the block of code to edit the preference in an initializer block which means it will be run before onCreate() where you actually assign gameData a value. That is what is leading to the NullpointerException.
As a rule you generally shouldn't do anything with an Activity until onCreate() is called. Especially if what you are doing requires a Context such as when dealing with preferences.
To fix this just move the code in the initializer block into the onCreate() method after you assign a value to gameData.
try this, you have to declare the file to save your data and after that you can only edit it.
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("MyPref", 0); // 0 - for private mode
editor = pref.edit();
i use commit() to update any data i want to save in SharedPreferences. As in your case is like
editor.putInt("xp", 0);
editor.putInt("mxp", 100);
editor.putInt("lv", 1);
editor.putInt("$", 0);
editor.putInt("$c", 0);
editor.commit();
i hope this will help you. Thanks.

Onclick button method in fragment

I created simple application in android. I have a user profile in navigation drawer. when choose address option in that drawer Open that fragment (Example:Address fragment). In that layout i put save button. While click that save button i want validate all the fields. But, when i choose that option from the drawer, application unfortunately stopped.
My code here:
AddressFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddressFragment extends Fragment {
EditText line2;
Button addrsave;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_address, container, false);
addrsave = (Button) getActivity().findViewById(R.id.addrsave);
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) view.findViewById(R.id.city_autoCompleteTextView);
// Get the string array
String[] city = getResources().getStringArray(R.array.city);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, city);
textView.setAdapter(adapter);
addrsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String addr = line2.getText().toString();
if (addr.equals("")) {
Toast.makeText(getActivity().getApplicationContext(), "Field cannot be left blank.", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
My logcat:
07-22 12:47:46.235 6355-6355/com.h2o E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.h2o, PID: 6355
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.h2o/com.h2o.AccountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.h2o.AddressFragment.onCreateView(AddressFragment.java:38)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5953)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Please anyone help!!
Thanks in advance!!!
Change this:
addrsave = (Button) getActivity().findViewById(R.id.addrsave);
to this:
addrsave = (Button) view.findViewById(R.id.addrsave);
Problem is that you are doing findViewById() on getActivity() which gives you a null. And then setting an onClickListener on it will throw NullPointerException.
Hope it helps! :)
Change this line
addrsave = (Button) getActivity().findViewById(R.id.addrsave);
to
addrsave = (Button) view.findViewById(R.id.addrsave);
Your R.layout.fragment_layout_address is assigned to view so you have to use view variable to get the button from layout. Secondly, Also initialize line2 which will throw an exception of NPE(Null Pointer) once you resolve the first issue
I cant see where you have initialized line2 ?
So line2 is null which might cause the exception
String addr = line2.getText().toString();
I think you have added button in fragment and you are trying to retrive it from activity. So change retrieving the view from getActivity() to view like below
addrsave = (Button) getActivity().findViewById(R.id.addrsave);
Replace the above with the following line
addrsave = (Button) view.findViewById(R.id.addrsave);
And You missed the line2 view initialization in line number 38

Categories