Sadly, you can't use JAR files to store Android resources. The generated R.java
will not contain informations from the JAR file.
findViewById return null, when export a project to jar file and import it into my project
-
23-09-2022 - |
Вопрос
I export a third party project to a jar file and set this project as a Library, then import it into my project. All"@+id" in xml files under folder layout have been modified to"@id". And I added ids.xml& public.xml file in the third party project. Mainifest.xml is added too.
In my project,code like that it.setClass(this, ActivityImageDetail.class);
It actually run into ActivityImageDetail.class, which is the third party class in jar file.
But when it execute layout = (LinearLayout) findViewById(R.id.***);
I got Null.
Logcat said java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mytest/com.MobileVisualSearch.ActivityImageDetail}: java.lang.NullPointerException
It seems that all findViewById()
in the third-party class return null
.
How could I fix it?
Решение
Другие советы
Could you post the code around the following line?
layout = (LinearLayout) findViewById(R.id.***);
Did you inflate the layout file before trying to get a particular id from it?
I made a small project that consumes a layout from a library in the following way:
View view = inflater.inflate(R.layout.imported_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.imported_textview);
textView.setText("Hey, I'm an imported TextView");
And imported_layout.xml is just a very simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/imported_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
Edit: this is possible because I've imported the library project but I'm not using it as an imported .jar.