![](/img/trans.png)
[英]Getting exception : selenium.NoSuchSessionException: Session ID is null. here i have used Localthread<webdriver> for parallel execution
[英]Android retrieving a TextView results in Null. Have setContentView; Used correct id; have activity in Maifest
我在運行時得到了一個nullPointerE,並研究了可能導致它的問題,而且我相信我都沒有遇到任何問題。
我相信問題是我的布局放在一個片段中,盡管在激活意圖時應用程序正確加載了布局,但我認為當setContentView時找不到該視圖。 我嘗試將片段設置為內容視圖,但是它也會崩潰。 另外,我在另一個屏幕上使用了一個片段,可以很好地訪問視圖。 我以為它可能是從意圖中訪問字符串,但是我確認當在onCreate方法中使用帶有if語句的findViewById時,我得到了一個空對象。 該應用程序運行正常(當然不執行內部代碼)。
這是我的課:
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent prev = getIntent();
String tipOfTheDay = prev.getStringExtra(Login.EXTRA_TIP);
String welcome = prev.getStringExtra(Login.EXTRA_WELCOME);
TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);
if (welcomeScreen != null) {
welcomeScreen.setText(welcome + "\n" + tipOfTheDay);
}
}
這是我的activity_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wingman.Home"
tools:ignore="MergeRootFrame" />
這是我的fragment_home.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wingman.Home$PlaceholderFragment" >
<TextView
android:id="@+id/textViewHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="@string/empty" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton1"
android:layout_below="@+id/imageButton1"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/q_button" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton3"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/calendar_button" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton2"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/tips_button" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton3"
android:layout_below="@+id/imageButton3"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/texts_button" />
這是我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wingman"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.wingman.Login"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.wingman.Home"
android:label="@string/title_activity_home" >
</activity>
</application>
感謝您的光臨。 我正在嘗試對碎片進行更多的研究,但老實說,我什至不知道這是否是原因。
您的TextView
在片段xml中,但是在您調用時
TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);
它正在activity_home.xml
尋找
試試這個來設置你的TextView
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_home, null)
TextView textView = (TextView) view.findViewById(R.id.textViewHome);
編輯您可以檢查此鏈接以獲取有關片段的教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.