基本的に、入力文字列と画像を下に出力するアクティビティを開始したいと思います。
activity_display_message.xml 内:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<TextView
android:text="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/serious" />
</LinearLayout>
これは OnCreate() のコードです。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
// Create the text view
TextView textView = (TextView)findViewById(R.id.textView);
//textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
// Show the Up button in the action bar.
setupActionBar();
}
このコードに何か問題がありますか?
編集: スタック トレースを取得する方法がわかりませんが、Catlog で次の行を見つけました:
07-05 12:26:53.677: E/AppsCustomizePagedView(611): Widget ComponentInfo{com.th.android.widget.gTabsimiClock/com.th.android.widget.gTabsimiClock.GTabsimiClock5x1} can not fit on this device (736, 144)
07-05 12:26:53.692: E/AppsCustomizePagedView(611): Widget ComponentInfo{at.abraxas.powerwidget.free/at.abraxas.powerwidget.free.SettingsAppWidgetProvider_5} can not fit on this device (760, 80)
07-05 12:26:57.958: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.146: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.630: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:27:07.833: E/AudioCache(131): Heap size overflow! req size 1052672, max size: 1048576
これはスタックトレースですか?
07-05 12:41:10.058: E/TAG(22474): STACKTRACE
07-05 12:41:10.058: E/TAG(22474): java.lang.NullPointerException
07-05 12:41:10.058: E/TAG(22474): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:41:10.058: E/TAG(22474): at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:41:10.058: E/TAG(22474): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:41:10.058: E/TAG(22474): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:41:10.058: E/TAG(22474): at android.os.Looper.loop(Looper.java:137)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:41:10.058: E/TAG(22474): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:41:10.058: E/TAG(22474): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:41:10.058: E/TAG(22474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:41:10.058: E/TAG(22474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:41:10.058: E/TAG(22474): at dalvik.system.NativeStart.main(Native Method)
実行時にクラッシュしたときのスタック トレース: (上記のものは、try / catch を実行して取得したものです)
07-05 12:52:24.386: E/AndroidRuntime(25496): FATAL EXCEPTION: main
07-05 12:52:24.386: E/AndroidRuntime(25496): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.os.Looper.loop(Looper.java:137)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:52:24.386: E/AndroidRuntime(25496): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:52:24.386: E/AndroidRuntime(25496): at dalvik.system.NativeStart.main(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496): Caused by: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:52:24.386: E/AndroidRuntime(25496): ... 11 more
メッセージが null ではありません。OnCreate を以下に変更すると、メッセージが出力されます: (もちろん、画像ではありません)
// Create the text view
//TextView textView = (TextView)findViewById(R.id.textView);
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
setContentView(textView);
回答: 間違いを見つけました: android:text="@+id/textView" 。android:text ではなく、android:id である必要があります ...
しかし、助けてくれてありがとう。皆さんのおかげで、私のような初心者向けの Android プログラミングの経験が大幅に改善されました。