Findviewbyid Returns Null

findViewByID returns null

which returns null

Possibly because you are calling it too early. Wait until onFinishInflate(). Here is a sample project demonstrating a custom View accessing its contents.

Why does findViewById(...) return null?

I had a similar issue and my problem was that findviewbyid only find child views. If the view is a sibling you'd have to call it from the parent. The activity should be able to find it.

Why does findViewById() returns null?

Your tab_connect etc. are included in fragment_main layout and not in activity_main layout that is inflated when you call findViewById() for those ids. The code you posted does not show where you're using fragment_main but what is certain is that it is not in your activity's view hierarchy at the time when you try to look up those views.

Either move the findViewById() calls and whatever you're doing with the views to the fragment, or move the views from the fragment to your main activity.

findViewById() returns null from a custom view

First you must create a custom_layout.xml and put your TextView and any widget you want to use like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

then initialize your custom_layout in init() methode of class GameMap :

public class GameMap extends LinearLayout {
.
.
.

private void init(Context context) {

View rootView = inflate(context, R.layout.sampel, this);
tv = rootView.findViewById(R.id.txt);
tv.setText("hello");
}

then add your custom_layout in your MainActivity layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<com.example.crosstheboxprova.GameMap
android:id="@+id/gameMapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>

</android.support.constraint.ConstraintLayout>

and in your MainActivity class :

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

GameMap gameMap = (GameMap) findViewById(R.id.gameMapView);

// And continue the code ...
}
}

Android: Findviewbyid() returns null

Your application is working on phones but not on tablets, this usually means a problem of resources.

The resources (layout, values, ...) used by Android depends on the device used. If there is a specific folder that match the device configuration (like large screen), the resource redefine in this folder will be used instead of the general folder.

There is multiple difference between phone/tablet (screen size, ratio, orientation, ...).

In your case, you might need to update or remove the layout main_activity.xml in a layout folder that match your tablet.

.findViewById() returns null

Check that the Switch class you imported in your java file is the correct one (e.g. the same one you used in the xml). This often happens with Toolbar (there are at least two, and if you import the wrong one and things don't work right/can't be cast and end up null).

kotlin findViewById returns null

when you do

imageView1 = view.findViewById(R.id.imageView)

you are looking for the view within any children that the button clicked has. You need to just do

imageView1 = findViewById(R.id.imageView)

How to resolve findViewByID() returning Null after setContentView()

Please use

addSiteButton =(Button) addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);


Related Topics



Leave a reply



Submit