0

私は致命的な例外に関する以前の投稿の多くを読みましたが、助けにはなりませんでした

だから私はAndroidに非常に慣れていないので、ボタンをクリックして別のページに移動し、他のボタンをクリックできる非常にシンプルなアプリを作成していますが、エミュレーターで実行すると、「アプリケーション」が表示されます。プロセスが予期せず停止しました」とlogcatは私に致命的な例外を与えますコードは

package my.first.pro;

import android.app.Activity;
import android.content.Intent;
 import android.os.Bundle;
  import android.view.*;
  import android.view.View.OnClickListener;

  public class MyfirstproActivity extends Activity  {
  /** Called when the activity is first created. */
  @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View button = this.findViewById(R.layout.main); 
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent i = new Intent ( MyfirstproActivity.this, Second.class );
        }
    });
}

}

XMLファイル

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >


  <Button
    android:id="@+id/button"
    android:layout_width="233dp"
    android:layout_height="wrap_content"
    android:text="view health care services " />

   </LinearLayout>

2番目のアクティビティ

   package my.first.pro;

   import android.app.Activity;
   import android.os.Bundle;

    public class Second extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
}

}

2番目のアクティビティxmlファイル

  <?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="wrap_content"
  android:baselineAligned="false"
  android:orientation="vertical" >

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" 
   >

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
       android:text="The following the health care services provided :" /> <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

         </TextView>


    <Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:text="General"/>
   </LinearLayout>


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
   android:orientation="vertical" 
    >

    <Button

      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:text="physciatric"/>
     </LinearLayout>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
    android:orientation="vertical" 
       >

    <Button

      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:text="pediatric"/>
     </LinearLayout>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
         >

       <Button

      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       android:orientation="vertical" 
      android:text="dental"/>
      </LinearLayout>
       </LinearLayout>

logcatは

  12-29 11:15:10.508: E/AndroidRuntime(438): FATAL EXCEPTION: main
    12-29 11:15:10.508: E/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activityComponentInfo{my.first.pro/my.first.pro.MyfirstproActivity}: java.lang.NullPointerException
    12-29 11:15:10.508: E/AndroidRuntime(438):  at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
     12-29 11:15:10.508: E/AndroidRuntime(438):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
     12-29 11:15:10.508: E/AndroidRuntime(438):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.os.Looper.loop(Looper.java:123)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at dalvik.system.NativeStart.main(Native Method)
    12-29 11:15:10.508: E/AndroidRuntime(438): Caused by: java.lang.NullPointerException
    12-29 11:15:10.508: E/AndroidRuntime(438):  at my.first.pro.MyfirstproActivity.onCreate(MyfirstproActivity.java:16)
     12-29 11:15:10.508: E/AndroidRuntime(438):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    12-29 11:15:10.508: E/AndroidRuntime(438):  ... 11 more
    12-29 11:15:24.829: E/AndroidRuntime(448): FATAL EXCEPTION: main
    12-29 11:15:24.829: E/AndroidRuntime(448): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.first.pro/my.first.pro.MyfirstproActivity}: java.lang.NullPointerException
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
   12-29 11:15:24.829: E/AndroidRuntime(448):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
   12-29 11:15:24.829: E/AndroidRuntime(448):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.os.Looper.loop(Looper.java:123)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at dalvik.system.NativeStart.main(Native Method)
    12-29 11:15:24.829: E/AndroidRuntime(448): Caused by: java.lang.NullPointerException
    12-29 11:15:24.829: E/AndroidRuntime(448):  at my.first.pro.MyfirstproActivity.onCreate(MyfirstproActivity.java:16)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at andr  oid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    12-29 11:15:24.829: E/AndroidRuntime(448):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    12-29 11:15:24.829: E/AndroidRuntime(448):  ... 11 more
4

4 に答える 4

3

ここ

View button = this.findViewById(R.layout.main); 

アクティビティレイアウトのビューを間違った方法で見つけようとしています。レイアウトxmlでビューを見つける適切な方法は次のとおりです。

 Button button = (Button)findViewById(R.id.button); 

また、すべてのUIコントロールを、メソッド内ではなくクラスレベルのフィールドとして宣言します。これは、すべての要素をクラスレベルで宣言すると、アクティビティ全体でアクセスできるためです。

編集:

アクティビティ全体でUI要素のアクセシビリティを向上させるために、常にこのアクティビティテンプレートを使用してUi要素を宣言または初期化します。

 public class MyfirstproActivity extends Activity  {
  /** Called when the activity is first created. */

  //******* ALWAYS DECLARE UI ELEMENTS HERE ********** //

Button button ; //<<< for example 


  @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   //** ALWAYS SET  layout FOR ACTIVITY BEFORE INITIALIZING UI ELEMENTS **
    setContentView(R.layout.main);

   //**** ALWAYS  INITILIZING UI ELEMENTS IN SIDE onCreate of Activity *** //
     button = (Button)findViewById(R.id.button); 

   }

}
于 2012-12-29T08:51:19.143 に答える
1

ボタンを間違った方法で宣言し、この行を変更し、

View button = this.findViewById(R.layout.main);

このような、

Button button = this.findViewById(R.id.button); 

また、2番目のAcitvityにこの行を追加する必要があります。

setContentView(int layoutid);

このような、

setContentView(R.layout.main) //First page layout

このメソッドは、アクティビティのレイアウトを表示するのに役立ちます。このメソッドは、ユーザーに表示するレイアウトまたはビューを決定します。

于 2019-01-06T16:30:22.773 に答える
0

この後に以下のステートメントを追加する必要があります

 Intent i = new Intent ( MyfirstproActivity.this, Second.class );

 startActivity(i);  //this will start the new activity.
于 2012-12-29T08:57:41.053 に答える
-1

行を変更します

View button = this.findViewById(R.layout.main);

これに:

View button = this.findViewById(R.id.button);
于 2012-12-29T09:04:40.887 に答える