3

私はアンドロイドが初めてで、これが私の最初のデモアプリです。あるページから別のページに移動しようとしています。しかし、メインのxmlファイルにボタンがあり、それを舐めると別のxmlの次のページに移動する別のページに移動できません。私は2つのJavaクラスを持っています:1st MianAcitvity、nextpagejava。2 xml : activity_main、nextpage

私のコード:マニフェスト

      <activity
      android:name="com.example.androiddemo.MainActivity"
      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="nextpagejava" ></activity>

MainActivity.java

package com.example.androiddemo;
  import android.os.Bundle; 
   import android.app.Activity; 
   import android.content.Intent; 
   import android.drm.DrmStore.Action;
  import android.view.Menu; 
   import android.view.View; 
  import android.view.View.OnClickListener; 
    import android.widget.Button;

    public class MainActivity extends Activity implements OnClickListener {

      @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          Button bt = (Button)findViewById(R.id.bnt);
           bt.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
                / / TODO Auto-generated method stub

             }
            }); 
            }

             @Override
            public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.activity_main, menu);
             return true;
            }

             @Override
            public void onClick(View v) {
              // TODO Auto-generated method stub
            switch(v.getId()){
              case R.id.bnt:
                     Intent in = new Intent(this, nextpagejava.class);
                     startActivity(in);
               break;


              }
           }
          }

活動_main.xml

           <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"
            tools:context=".MainActivity" >

               <TextView 
           android:id="@+id/clicktxt"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Click"        
         />

           <Button
           android:id="@+id/bnt"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
           android:text="Click Me"
          />

nextpage.xml

           <TextView 
             android:layout_width="match_parent"
              android:layout_height="wrap_content"
               android:text="I am i a next page..."
             />

            <Button 
              android:id="@+id/btn1"
               android:layout_width="match_parent"
                android:layout_height="wrap_content"
              android:text="Back"
             />

次のページjava.java

         package com.example.androiddemo;

          import android.app.Activity;
          import android.os.Bundle;
          import android.view.View;
          import android.view.View.OnClickListener;
            import android.widget.Button;

           public class nextpagejava extends Activity implements OnClickListener{

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

            Button bt = (Button)findViewById(R.id.btn1);
            bt.setOnClickListener(this);
            }

          @Override
           public void onClick(View v) {
            // TODO Auto-generated method stub

        }
         }

「残念ながら、androiddemo が停止しました」というメッセージがポップアップとして表示されます。

このエラーが発生する理由を誰か教えてください。また、行ごとのデバッグ ログを見つけることができる場所を教えてください。

4

4 に答える 4

2

MainActivity.javaこれをファイルから削除するだけです。

  bt.setOnClickListener(new View.OnClickListener() {   
       @Override   
       public void onClick(View v) {
            / / TODO Auto-generated method stub
         }
        }); 

このように書く代わりに:

bt.setOnClickListener(this);

またManifest、以下のようにファイルの変更を行います。

<activity android:name=".nextpagejava" ></activity>

次に、チェックアウトします。今はうまくいくはずだと思います。

于 2013-01-09T10:54:48.463 に答える
1

2 つの onClick() 関数があります。2 番目のものを削除し、ボタンのリスナー内に次の 2 行を追加します。

  Intent in = new Intent(this, nextpagejava.class);
  startActivity(in);

それはあなたを動かし続けるはずです。それでもエラーが続く場合は、logcat を投稿してください

于 2013-01-09T10:45:46.053 に答える
1

スタック トレースから多くの情報が得られるので、ここに記載します。

しかし、宣言の欠落に関連している可能性が非常に高いと思います(たとえば、初期化されていないボタンをクリックしようとするなど)。非常に一般的な問題。

編集:おそらくインテントを使用すると問題が解決するはずです。

于 2013-01-09T10:46:00.140 に答える