应用窗口自动关闭

时间:2018-02-07 09:41:05

标签: android android-studio

当我点击一个按钮在我的Android应用程序中打开一个窗口时,新窗口会在几秒后自动关闭,然后它会返回到主窗口。我正在尝试打开MainSig窗口。我是Android Studio的新手,我刚刚开始,我需要帮助。我还在清单中添加了所有活动。

这是我的代码

    package com.orig3.thesis;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class Main2Activity extends AppCompatActivity {
public Button button123;
public Button button456;
    public void init(){


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init();
        button123 = (Button)findViewById(R.id.button4);
        button123.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent toy = new Intent(Main2Activity.this, MainActivity.class);
                startActivity(toy);
            }
        });
        button456 = (Button)findViewById(R.id.button);
        button456.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent toy = new Intent(Main2Activity.this, MainSig.class);
                startActivity(toy);
            }
        });
    }
}

这是MainSig代码

package com.orig3.thesis;


        import android.content.pm.ActivityInfo;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;

        import com.github.gcacace.signaturepad.views.SignaturePad;

public class MainSig extends AppCompatActivity {
    SignaturePad signaturePad;
    Button saveButton, clearButton;

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

        signaturePad = (SignaturePad)findViewById(R.id.signaturePad);
        saveButton = (Button)findViewById(R.id.saveButton);
        clearButton = (Button)findViewById(R.id.clearButton);

        //disable both buttons at start
        saveButton.setEnabled(false);
        clearButton.setEnabled(false);

        //change screen orientation to landscape mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
            @Override
            public void onStartSigning() {

            }

            @Override
            public void onSigned() {
                saveButton.setEnabled(true);
                clearButton.setEnabled(true);
            }

            @Override
            public void onClear() {
                saveButton.setEnabled(false);
                clearButton.setEnabled(false);
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //write code for saving the signature here
                Toast.makeText(MainSig.this, "Signature Saved", Toast.LENGTH_SHORT).show();
            }
        });

        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signaturePad.clear();
            }
        });
    }
}

清单:)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.orig3.thesis">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>

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

    </application>

</manifest>

Logcat:)

02-07 19:07:41.980 28831-28831/com.orig3.thesis I/art: Late-enabling -Xcheck:jni
02-07 19:07:41.980 28831-28831/com.orig3.thesis I/art: Reinit property: dalvik.vm.checkjni= false

                                                       [ 02-07 19:07:41.997 28840:28840 I/         ]
                                                       power log dlsym ok
02-07 19:07:42.287 28831-28831/com.orig3.thesis W/System: ClassLoader referenced unknown path: /data/app/com.orig3.thesis-1/lib/arm64
02-07 19:07:42.301 28831-28831/com.orig3.thesis I/InstantRun: starting instant run server: is main process
02-07 19:07:42.357 28831-28831/com.orig3.thesis I/HwCust: Constructor found for class android.app.HwCustActivityImpl
02-07 19:07:42.444 28831-28831/com.orig3.thesis W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-07 19:07:42.623 28831-28831/com.orig3.thesis I/BitmapFactory: set decoder allocator to gralloc
02-07 19:07:42.761 28831-28831/com.orig3.thesis I/HwSecImmHelper: mSecurityInputMethodService is null
02-07 19:07:42.776 28831-28831/com.orig3.thesis I/HwPointEventFilter: do not support AFT because of no config
02-07 19:07:42.856 28831-28894/com.orig3.thesis I/OpenGLRenderer: Initialized EGL, version 1.4
02-07 19:07:42.868 28831-28894/com.orig3.thesis W/linker: /vendor/lib64/libhwuibp.so: unused DT entry: type 0xf arg 0xe3a
02-07 19:07:42.895 28831-28831/com.orig3.thesis W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

1 个答案:

答案 0 :(得分:0)

为“风景”加入你的宣言:

<activity android:name=".MainSig "
android:label="@string/app_name"
android:screenOrientation="landscape" />

并从java类中删除此行。

//change screen orientation to landscape mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);