2014-04-18 205 views
1

我在Android 16(4.1)應用程序上使用setText()方法時出現問題。我試圖編程任何東西到TextView tvopis(這裏只是一些測試字符串,後來我打算從意圖把那裏的字符串)。 當我打開活動opis時,應用程序崩潰。如果我評論setText()部分,一切工作正常。android setText崩潰我的應用程序

這裏的Java代碼

package com.example.pierwszyandroid; 

import android.app.Activity; 
import android.app.ActionBar; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.os.Build; 

public class OpisActivity extends Activity { 

    public TextView tvopis; 
    String txt; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_opis); 
     tvopis = (TextView) findViewById(R.id.tvotest); 

     tvopis.setText("txt test"); 


     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
    } 

這裏的fragment_opis.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.pierwszyandroid.OpisActivity$PlaceholderFragment" > 

    <TextView 
     android:id="@+id/tvotest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

,這是activity_opis.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.pierwszyandroid.OpisActivity" 
    tools:ignore="MergeRootFrame" /> 

和這裏的logcat的錯誤消息:

04-18 10:44:59.665: I/Choreographer(723): Skipped 206 frames! The application may be doing too much work on its main thread. 
04-18 10:46:34.016: I/Choreographer(723): Skipped 41 frames! The application may be doing too much work on its main thread. 
04-18 10:46:34.135: D/AndroidRuntime(723): Shutting down VM 
04-18 10:46:34.135: W/dalvikvm(723): threadid=1: thread exiting with uncaught exception (group=0x2bc9a300) 
04-18 10:46:34.155: E/AndroidRuntime(723): FATAL EXCEPTION: main 
04-18 10:46:34.155: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pierwszyandroid/com.example.pierwszyandroid.OpisActivity}: java.lang.NullPointerException 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.os.Looper.loop(Looper.java:137) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-18 10:46:34.155: E/AndroidRuntime(723): at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 10:46:34.155: E/AndroidRuntime(723): at java.lang.reflect.Method.invoke(Method.java:511) 
04-18 10:46:34.155: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-18 10:46:34.155: E/AndroidRuntime(723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-18 10:46:34.155: E/AndroidRuntime(723): at dalvik.system.NativeStart.main(Native Method) 
04-18 10:46:34.155: E/AndroidRuntime(723): Caused by: java.lang.NullPointerException 
04-18 10:46:34.155: E/AndroidRuntime(723): at com.example.pierwszyandroid.OpisActivity.onCreate(OpisActivity.java:25) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.Activity.performCreate(Activity.java:5008) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
04-18 10:46:34.155: E/AndroidRuntime(723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
04-18 10:46:34.155: E/AndroidRuntime(723): ... 11 more 

任何幫助將非常感激,因爲我找不到任何已發佈的類似問題的答案。

+0

logcat的:'的onCreate(OpisActivity.java:25 )'=第25行onCreate東西返回null。這顯然是'tvopis'。 – Fllo

回答

0

佈局您已經設置錯誤您的佈局在onCreate()方法。您TextView駐留在佈局fragment_opis.xml內,而您已將佈局設置爲setContentView(R.layout.activity_opis);

setContentView(R.layout.activity_opis);線只要改變

爲以下

setContentView(R.layout. fragment_opis); 
+0

設置 setContentView(R.layout。fragment_opis);也崩潰它 –

+0

只是評論你的'if(savedInstanceState == null){......'條件在你的代碼@ N.M.W – GrIsHu

+0

謝謝,解決了它:) –

1

如果您查看log cat,當您嘗試將文本設置爲文本視圖(OpisActivity中的第25行)時,您會看到您有空指針異常。

這意味着在佈局中找不到txt視圖,因此無法初始化。由於您的textview位於片段中,因此您必須先將片段添加到佈局,然後查找該視圖。

解決您的問題,該片段初始化後移動findById和方法的setText在onCreate方法,像這樣:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_opis); 

    if (savedInstanceState == null) { 
     getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
    } 

    tvopis = (TextView) findViewById(R.id.tvotest); 
    tvopis.setText("txt test"); 
} 
+0

它沒有幫助,應用程序仍然像以前 –

+0

崩潰,那麼你的PlaceholderFragment沒有正確設置。您是否將PlaceholderFragment中的contentView設置爲fragment_opis.xml? – peshkira

+0

我已將setContentView更改爲(R.layout.fragment_opis)並將其刪除(sevedInstancestate == null){...},現在它工作正常。 –

1

您的應用程序崩潰,因爲你的TextView tvotest屬於fragment_opis.xml佈局和你必須將activity_opis.xml文件設置爲您的活動。

所以初始化PlaceholderFragment()先初始化您的TextView tvotest或更改像

setContentView(R.layout.fragment_opis.xm) 
0

改變這種setContentView(R.layout.activity_opis);

這個

setContentView(R.layout.fragment_opis.xml);