我在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
任何幫助將非常感激,因爲我找不到任何已發佈的類似問題的答案。
logcat的:'的onCreate(OpisActivity.java:25 )'=第25行onCreate東西返回null。這顯然是'tvopis'。 – Fllo