Unable to properly reference TextView from java within same activity - java

I am running a basic application that passes information between two screens, but I'm crashing as soon as I select to move to the second activity.
Commenting out blocks of code, I have narrowed the problem down to a NULL object.
XML File "activity_two"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.id11434343.exercise2.ActivityTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hintName"
android:id="#+id/textViewName"
android:layout_below="#+id/textViewHeader"
android:layout_marginTop="15dp"/>
Java File "ActivityTwo"
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent parentIntent = getIntent();
String name = parentIntent.getStringExtra(ActivityOne.NAME_MESSAGE);
TextView finalName = (TextView) findViewById(R.id.textViewName);
finalName.setText(name);
}
When I run TextView finalName = (TextView) findViewById(R.id.textViewName);, it passes a null reference. I have a similar line in the first activity, EditText name = (EditText) findViewById(R.id.editTextName);, but experience no problem.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference com.id11434343.exercise2.ActivityTwo.onCreate ActivityTwo.java:34)"
I receive this error, and have tried debug to get a closer look. My EditText name = null, however, the R.id.editTextName = 2131558488, which appears to be a valid ID, in line with the IDs from other elements. I have found a couple of similar questions, on here, but they previously appear to have come down to simple errors, such as using incorrect references, so I double checked my variables, and compared it directly to the working version, to ensure I was not missing something small.
Am I missing something here? I can't understand why I'm having troubles with a ViewText, where I didn't have any problems doing the same thing with an EditText.

This passes null when you use a wrong layout in setContentView() means that the given id doesn't exist in the given layout.

Related

Unknown bits set in runtime_flags: 0x28000

I made a simple app using Cards in Material Components in Android. When I try to run it in my "OnePlus AC2001" mobile, I'm getting this error in logcat.
2021-01-31 17:28:59.544 26933-26933/? E/rialdesigncard: Unknown bits set in runtime_flags: 0x28000
But I'm not getting any error when I run the app in the emulator.
This is my MainActivity.java file:
public class MainActivity extends AppCompatActivity {
CardView cardView;
TextView textView;
CheckBox checkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
cardView = findViewById(R.id.my_card_ui);
textView = findViewById(R.id.title_text);
checkBox = findViewById(R.id.checkbox);
cardView.setOnClickListener(v -> {
checkBox.setChecked(checkBox.isChecked()? false: true);
textView.setText("First title");
});
}
}
This is my ActivityMain.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/my_card_ui"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkable="true"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="30dp">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title 1"
android:textColor="#color/black"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/title_para"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I am a paragraph and I am in a card. I am a paragraph and I am in a card. I am a paragraph and I am in a card."
android:textColor="#color/purple_700"
android:textSize="20dp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
I couldn't find the solution to this error anywhere. Please help me solve this.
I have been consistently seeing this error for some time now (OnePlus device), but I haven't been able to find a solution either. The error does not seem to impact the app in any way though.
Additional point of information:
I have observed that this error appears in the Logcat whenever I Run/Debug the app on a real device, but it does not show up consistently in the Run/Debug consoles. I haven't tried to check (as you have) if the error is replicated when using an emulator.
Also, my Logcat shows the Unknown bits set in runtime_flags: 0x20000 error for many other packages as the device is running. Some examples: .android.dialer, oneplus.weather, twitter.android, and so on.
This is what I've found out so far:
From the ARM DS5 DUI0446U Debugger User Guide, a possible memory map for internal RAM can be between 0x8000 and 0x28000. I'm not from a computer engineering background, and I'm not sure if this is a relevant direction for understanding the problem further. Searching the Android Runtime (ART) site (this, or in general, this) yielded no useful results.
For a related error, by following this answer to the ART source code, 0x28000 seems to be related to either the DEBUG_IGNORE_APP_SIGNAL_HANDLER flag or the DISABLE_TEST_API_ENFORCEMENT_POLICY flag (line 154-155 as of this time of this post). [E.g., 1 << 17 is 0x20000]
I don't have a deeper understanding of the either of the two flags. Nevertheless, the 0x20000 error seems to be pretty commonplace in production apps. While this is not an indication that the 0x28000 error is equally hassle-free, to my mind it does shed some light on the nature of the runtime_flags.
It just might be the case that errors related to these flags (see the enum in the source code) may not really "break" the app. Some further information is required in order to be absolutely certain, but the above might be good enough to not be concerned about the app breaking.
P.S.: If you think this response is good enough, do mark it as an answer so I will have some feedback as well.

Data binding: Missing required view with ID

I create an app using binding and I get an error that I couldn't solve.
The error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newtest/com.example.mylibrary.MainActivity1}: java.lang.NullPointerException: Missing required view with ID: com.example.newtest:id/myRecyclerview
Thanks!
public class MainActivity1 extends AppCompatActivity {
RecyclerView.LayoutManager layoutManager;
ArrayList<POJOimage>pojoImageArrayList;
ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
layoutManager = new LinearLayoutManager(this);
binding.myRecyclerview.setLayoutManager(layoutManager);
init();
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity1"
android:background="#000"
android:padding="10dp">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myRecyclerview"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I strongly suspect some Android Studio bug.
I had the same bug, and everything I've had done was adding some TextView.
As it was easy to find this difference, I've just started fiddling with this TextView in xml. At some point bug disappeared.
And here comes the best. Before starting to deal with the problem I've saved problematic change with git. So at the end I've reapplied this change and ... everything worked. No more bug !
P.S. As this bug reappears, I've found that the easiest solution for getting rid of it is to use "Clean Project" from "Build" menu.

Application randomly bombed, WebView throws null pointer [duplicate]

This question already has an answer here:
android webview causing null pointer
(1 answer)
Closed 7 years ago.
I've reduced my application to an almost dumb state trying to figure out the issue. I'm using the AndroidStudio (IntelliJ version) for development. I've been developing just fine until just recently the application started crashing before launching it seemed. Logcat showed this:
NullPointerException: Attempt to invoke virtual method
'void android.webkit.WebView.loadUrl(java.lang.String)'
on a null object reference
As stated I've reduced the application to an almost pathetic state. (No services, No receivers), and here's my Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
browser = (WebView) findViewById(R.id.web_view);
browser.loadUrl("file:///android_asset/www/index.html");
}
The line calling the error is the "browser.loadUrl" file, and this has been working fine for the last few hours. Please note that "requestWindowFeature" must be called above super.onCreate or the application will crash.
Here's my activity (Also unedited) and my application is nothing but a webview anyway.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
I'm a bit confused.
you missed setContentView(int layoutId). Without calling it your Activity has no widgets to look for, and findViewById returns null
You need to add setContentView() in onCreate() method for Activity.
After setting up content view, find reference of id will be get in Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.<YOUR_LAYOUT_ID>);
browser = (WebView) findViewById(R.id.web_view);
browser.loadUrl("file:///android_asset/www/index.html");
}

My app crashes because I declared a checkbox equal to a checkbox

When I open this activity my app crashes and says that it is due to this line of code:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);
The layout for alarm_details_repeat_weekly is right here:
<CheckBox
android:id="#+id/alarm_details_repeat_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/alarm_details_label_wednesday"
android:layout_alignBottom="#+id/alarm_details_label_wednesday"
android:layout_alignParentRight="true"
android:checked="true" />
Since some people asked, here is the logcat:
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:2083)
at minutemovement.mintemovement.com.minutemovement.AlarmDetailsActivity.<init>(AlarmDetailsActivity.java:28)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
Here is the full code: http://pastebin.com/EygRhAj4
You have a typo. You declared your id as alarm_details_repeat_wednesday in your xml but then tried to get an object with id alarm_details_repeat_weekly. Change your code to this:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_wednesday);
Try remove the private,
CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly); or declare the private CheckBox outside oncreate and initialize the object this way: chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);

NullPointerException when acting on a View

For some reason I'm getting a nullPointerException when working with any new View I place in my XML. The view type (TextView, EditText, etc) doesn't matter. Any views I originally had work - it's isolated to any newly added views.
I've tried cleaning the project numerous times, deleted the entire XML file, restarted eclipse, then re-pasted the XML back into a new file, no luck. Appears to be similar as this question, but nothing has gotten this working.
I really don't want to have to re-create the entire project, but I'm not sure what else to do if recreating the XML and cleaning isn't enough.
Code is as follows:
XML
...
<TextView
android:id="#+id/dlg_add_proj_test_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Text" />
...
Activity
...
public void fireDlg() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_add_proj);
dialog.setTitle("Add Project");
//Other previous views
TextView newTxtView = (TextView) findViewById(R.id.dlg_add_proj_test_text);
newTxtView.setText("New Text"); //Null Pointer Here
}
...
you should be using dialog.findViewById(R.id.dlg_add_proj_test_text) instead of just findViewById. The findViewById uses the activity's method while dialog.findViewById uses the method in the dialog.

Categories