android app crash when i call EditText [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
The application crashes when I call EDITTEXT.
layout
<EditText
android:id="#+id/T_Price"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/B_LessMoney"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:text="#string/Price"
android:visibility="visible" />
Main
EditText mPrice = (EditText) findViewById(R.id.T_Price);
and this the report
E/AndroidRuntime: FATAL EXCEPTION: main
Process: budgetreport.com.budgetreport, PID: 2791
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{budgetreport.com.budgetreport/budgetreport.com.budgetreport.Report}: 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:2488)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
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.(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.(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 budgetreport.com.budgetreport.Report.(Report.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5765) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
I/Process: Sending signal. PID: 2791 SIG: 9
Application terminated.

You are calling findViewById before Activity's onCreate method (in the constructor?).
What you should do is to initialize it in the Activity's onCreate:
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mPrice = (EditText) findViewById(R.id.T_Price);
}

Related

How to deal with about invoke virtual method 'void android.view.View.setBackground(Drawable)' on a null object reference [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
This is my source code,it seams the program throw exception at line 41.
public class LaunchActivity extends ACtivity{
//Drawable
private ColorDrawable mBgDrawable;
#Override
protected int getContentLayoutId() {
return R.layout.activity_launch;
}
#Override
protected void initWidows() {
super.initWidows();
//拿到根布局
View root = findViewById(R.id.activity_launch);
//获取颜色
int color = UiCompat.getColor(getResources(),R.color.colorPrimary);
//创建一个Drawable
ColorDrawable drawable = new ColorDrawable(color);
//设置给背景
root.setBackground(drawable); **//line 41**
mBgDrawable = drawable;
}
the layout source code (activity_launch.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_launch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context="net.qiujuer.italker.push.LaunchActivity">
.....
and this is the error information.
(Does it means the View ('root') I get is a null object ?)
FATAL EXCEPTION: main
Process: net.qiujuer.italker.push, PID: 3046
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.qiujuer.italker.push/net.qiujuer.italker.push.LaunchActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2925)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:110)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at net.qiujuer.italker.push.LaunchActivity.initWidows(LaunchActivity.java:41)
at net.qiujuer.italker.common.app.ACtivity.onCreate(ACtivity.java:16)
at android.app.Activity.performCreate(Activity.java:7130)
at android.app.Activity.performCreate(Activity.java:7121)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2905)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:110) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6649) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 
This is my first attempt to ask questions on stack overflow. Thank you for your help.
Seems that you have declared layout name in root-view so view is not initialized.
Please check findViewById(R.id.activity_launch) for rootview and updated accordingly.

Calling onSaveInstanceState on Activity

I inherited a project, trying to understand and solve an old bug.
When I start an activity and let it sit for a bit (sometimes it's almost immediate), but it appears to go into a pause state or something and attempts to reload the activity and gets an error.
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ... ' on a null object reference"
I found that when I add this to my activity:
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
//super.onSaveInstanceState(savedInstanceState);
}
It appears to work perfectly.
However, when I uncomment the line above to super.onSaveInstanceState(savedInstanceState);, I get the error again.
My activity is extending the ActionBarActivity, if that is helpful.
Any help would be appreciated. Thank you!
Related Code:
#Override
protected void prepareLayout() {
textMessage = (TextView) findViewById(R.id.textMessage);
textNotes = (EditText) findViewById(R.id.textNotes);
textMessage.setText(getOneOfMyItems().getText());
// ... the above line is where the problem error is thrown.
}
Stack Trace:
Process: com.myproj.myproj, PID: 9272
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproj.myproj/com.myproj.activities.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433)
at android.app.ActivityThread.access$1200(ActivityThread.java:226)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7270)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at com.myproj.xyz.MyFragment.prepareLayout(MyFragment.java:44)
at com.myproj.fragments.BaseFragment.prepareAndSetView(BaseFragment.java:49)
at at com.myproj.xyz.BaseMyFragment.onCreateView(BaseMyFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1129)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:615)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
at android.app.Activity.performStart(Activity.java:6943)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433) 
at android.app.ActivityThread.access$1200(ActivityThread.java:226) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7270) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

unfortunately ___ has stopped , but logcat contains many errors

i have developped an application android that contains two activities , the first activity contains two Edittext and a login button , i have to enter the value 'abcd' as a username and 'EFGH' as a password and validate with the button , the probleme is when i clicked login the application has crashed ,
the error message is : unfortunately ___ is stopped
logcat: errors 11-15 19:22:56.237 783-783/? E/Zygote: accessInfo : 0
11-15 19:24:41.057 783-783/info.adem.tplogin E/AndroidRuntime: FATAL
EXCEPTION: main
Process: info.adem.tplogin, PID: 783
java.lang.RuntimeException: Unable to start activity
ComponentInfo{info.adem.tplogin/info.adem.tplogin.FriendActivity}:
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:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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 info.adem.tplogin.FriendActivity.onCreate(FriendActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object
Before you use a reference from your button to add an event, you need to retrieve the reference from it.
Like this:
public class YourActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_layout)
Button button = (Button)findViewById(R.id.your_button_id)
//button.setOnClickListener........
}
}

android.content.res.Resources android.content.Context.getResources() error when using github repository

I am using https://github.com/BlacKCaT27/CurrencyEditText to enter currency into my application and I am using this code:
CurrencyEditText cet = new CurrencyEditText(this, null);
Here is the logcat:
Process: com.dharquissandas.budget, PID: 17620
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dharquissandas.budget/com.dharquissandas.budget.add_expense}: 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:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
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.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:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.view.View.<init>(View.java:4306)
at android.view.View.<init>(View.java:4454)
at android.widget.TextView.<init>(TextView.java:995)
at android.widget.EditText.<init>(EditText.java:79)
at android.widget.EditText.<init>(EditText.java:75)
at android.widget.EditText.<init>(EditText.java:71)
at com.blackcat.currencyedittext.CurrencyEditText.<init>(CurrencyEditText.java:33)
at com.dharquissandas.budget.add_expense.<init>(add_expense.java:29)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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) 
What am I doing wrong I don't know, I am fairly new to android programming can someone help me out. What am I supposed to put in the attribute part of the code?
Here is my code: https://pastebin.com/57mTUMWX
You should not be creating view elements in your Code.
Add the view for CurrencyText to your XML.
Then you make sure it has an #+id/myName and find it by doing
CurrencyText myText = findViewById(R.id.myName);

Navigation drawer errors [duplicate]

This question already has an answer here:
How to set text to view from drawer header layout in navigation drawer without inflating view
(1 answer)
Closed 4 years ago.
i tried running an app in android studio but i keep getting this weird error.
I hardly could understand what this error is all about and how to fix this, your help would be much appreciated.
here is the logcat
06-16 15:26:02.141 5270-5270/com.sensiblewallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sensiblewallet, PID: 5270
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sensiblewallet/com.sensiblewallet.ActivityMainWallet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5481)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.sensiblewallet.ActivityMainWallet.onCreate(ActivityMainWallet.java:212)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5481) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
here is the piece of code it is indicating to.
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.END);
mDrawerLay = (NavigationView)findViewById(R.id.navigation_view);
mDrawerLay.setNavigationItemSelectedListener(this);
TextView navigationTotalCreditTextView = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_total_credits);
navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext,
PreferenceConnector.WALLETPOINTS, 0));
TextView navigationWalletIDText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
PreferenceConnector.WALLETID, ""));
TextView navigationUsernameText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
PreferenceConnector.USERNAME, ""));
// MyUtils.setFontAllView((RelativeLayout)findViewById(R.id.laymenu));
Try this code :
View v = mDrawerLay.getHeaderView(0);
TextView navigationTotalCreditTextView = (TextView)v.findViewById(R.id.nav_drawer_total_credits);
TextView navigationWalletIDText = (TextView)v.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
PreferenceConnector.WALLETID, ""));
TextView navigationUsernameText = (TextView)v .findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
PreferenceConnector.USERNAME, ""));

Categories

Resources