每次我从一个活动转移到另一个活动时,我的应用程序崩溃

时间:2013-12-14 01:51:13

标签: android android-activity android-manifest

伙计我需要你的帮助,我创建了一个包含3个活动的应用程序(主要,线性和表格),如下所示

MainActivity

 package com.hossa.layoutapp;
       import android.app.Activity;
       import android.content.Context;
       import android.content.Intent;
       import android.os.Bundle;
       import android.view.Menu;
       import android.view.View;
       import android.view.View.OnClickListener;
       import android.widget.Button;
       import android.widget.EditText;
       import android.widget.Toast;

       public class MainActivity extends Activity {
       Button Submit1,Table1,Linear1;
       EditText Name1,Email1,Phone1;



       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Submit1=(Button)findViewById(R.id.RelativeSubmit);
        Table1=(Button)findViewById(R.id.RelativeTable);
        Linear1=(Button)findViewById(R.id.RelativeLinear);
        Name1=(EditText)findViewById(R.id.Name1);
        Email1=(EditText)findViewById(R.id.Email1);
        Phone1=(EditText)findViewById(R.id.Phone1);

        Submit1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //providing the user with help explaination//
                Context context=getApplicationContext();
                String message1="your data is submitted";
                int Duration=Toast.LENGTH_LONG;
                Toast t=Toast.makeText(context,message1, Duration);
                t.show();
            }
        });
        Table1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,TableActivity.class);
                   startActivity(i);
            }
        });
        Linear1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,LinearActivity.class);
                   startActivity(i);
            }
        });
       }

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

       }

下面是 LinearActivity

           package com.hossa.layoutapp;

           import android.app.Activity;
           import android.content.Context;
           import android.content.Intent;
           import android.os.Bundle;
           import android.view.Menu;
           import android.view.View;
           import android.view.View.OnClickListener;
           import android.widget.Button;
           import android.widget.EditText;
           import android.widget.Toast;

           public class LinearActivity {

        public class MainActivity extends Activity {
            Button Submit2,Table2,Relative1;
            EditText Name2,Email2,Phone2;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.linearlayout);
                Submit2=(Button)findViewById(R.id.LinearSubmit);
                Table2=(Button)findViewById(R.id.LinearTable);
                Relative1=(Button)findViewById(R.id.LinearRelative);
                Name2=(EditText)findViewById(R.id.Name2);
                Email2=(EditText)findViewById(R.id.Email2);
                Phone2=(EditText)findViewById(R.id.Phone2);
                Submit2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //providing the user with help explaination//
                        Context context=getApplicationContext();
                        String message1="your data is submitted";
                        int Duration=Toast.LENGTH_LONG;
                        Toast t=Toast.makeText(context,message1, Duration);
                        t.show();
                    }
                });
                Table2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent j=new     Intent(getApplicationContext(),TableActivity.class);
                        startActivity(j);
                    }
                });
                Relative1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent k=new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(k);
                    }
                });
            }

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

        }


       }

最后是表活动

    package com.hossa.layoutapp;

   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.Menu;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;
   import android.widget.EditText;
   import android.widget.Toast;

   public class TableActivity {

public class MainActivity extends Activity {
    Button Submit3,Relative2,Linear2;
    EditText Name3,Email3,Phone3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablelayout);
        Submit3=(Button)findViewById(R.id.TableSubmit);
        Relative2=(Button)findViewById(R.id.TableRelative);
        Linear2=(Button)findViewById(R.id.TableLinear);
        Name3=(EditText)findViewById(R.id.Name3);
        Email3=(EditText)findViewById(R.id.Email3);
        Phone3=(EditText)findViewById(R.id.Phone3);
        Submit3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //providing the user with help explaination//
                Context context=getApplicationContext();
                String message1="your data is submitted";
                int Duration=Toast.LENGTH_LONG;
                Toast t=Toast.makeText(context,message1, Duration);
                t.show();
            }
        });
        Linear2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent j=new     Intent(getApplicationContext(),LinearActivity.class);
                startActivity(j);
            }
        });
        Relative2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent k=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(k);
            }
        });
    }

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

}


    }

清单文件:          

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.hossa.layoutapp.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="com.hossa.layoutapp.LinearActivity"
        android:label="@string/app_name"
        >
        </activity>
        <activity
        android:name="com.hossa.layoutapp.TableActivity"
        android:label="@string/app_name"
         >
        </activity>
</application>

</manifest>

要做的主要事情是按下一个按钮并移动到下一个活动,但它总是崩溃...请帮忙thx

3 个答案:

答案 0 :(得分:3)

好的,这可能是也可能不是答案,但我不确定我是否可以在评论中指出这一点,这是非常错误和非常令人困惑的。

在您的课堂声明中,例如

public class LinearActivity {

public class MainActivity extends Activity {

我甚至不确定该怎么想,但没有理由或想要在每个MainActivity中加入Activity。你有MainActivity

public class MainActivity extends Activity {

然后你的另一个Activities应该以相同的方式声明,但是有自己的类名,例如。

public class LinearActivity extends Activity {

解决此问题,如果它无法解决问题,请从崩溃中发布您的logcat。

答案 1 :(得分:0)

你的代码中有2个大错误 - 1s是该活动不应该是内部类,如果你的类LinearActivity是活动而不是你应该从Activity扩展它。第二个是你的时候 试图打电话给其他活动

 Intent j=new     Intent(getApplicationContext(),LinearActivity.class);
            startActivity(j);

你总是会收到一个错误,因为LinearActivity.class不是一个Activity。

要解决您的问题,只需从Activity扩展LinearActivity并删除内部MainActivity类

答案 2 :(得分:0)

您在表活动中使用该代码并且线性活动是错误的

public class TableActivity {

public class MainActivity extends Activity {

使用public class TableActivity extends Activity {

public class Linearactivity extends Activity {