在android中传递意图或更改活动所导致的崩溃

时间:2015-03-12 15:43:35

标签: android android-intent android-activity logcat

我不确定这个错误究竟发生在哪里。

我的主菜单链接到其他活动时使用了意图没有问题

我正在尝试为我的应用程序的连接设置接受两个值,然后将其传递给另一个活动,以便它可以连接到服务器。我不完全确定出了什么问题。

03-12 15:31:59.588    5671-5671/killianmills.pycast W/ApplicationPackageManager﹕ getCSCPackageItemText()
03-12 15:31:59.718    5671-5671/killianmills.pycast I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
    OpenGL ES Shader Compiler Version: 17.01.11.SPL
    Build Date: 01/17/14 Fri
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
03-12 15:31:59.758    5671-5671/killianmills.pycast D/OpenGLRenderer﹕ Enabling debug mode 0
03-12 15:31:59.858    5671-5671/killianmills.pycast V/RenderScript﹕ 0x79275f50 Launching thread(s), CPUs 4
03-12 15:32:31.018    5671-5671/killianmills.pycast W/ApplicationPackageManager﹕ getCSCPackageItemText()
03-12 15:32:33.921    5671-5671/killianmills.pycast D/dalvikvm﹕ newInstance failed: Lkillianmills/pycast/GeneralMode; not accessible to Landroid/app/Instrumentation;
03-12 15:32:33.921    5671-5671/killianmills.pycast D/AndroidRuntime﹕ Shutting down VM
03-12 15:32:33.921    5671-5671/killianmills.pycast W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4189dda0)
03-12 15:32:33.931    5671-5671/killianmills.pycast E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: killianmills.pycast, PID: 5671
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{killianmills.pycast/killianmills.pycast.GeneralMode}: java.lang.IllegalAccessException: access to class not allowed
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalAccessException: access to class not allowed
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1208)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
03-12 15:32:35.833    5671-5671/killianmills.pycast I/Process﹕ Sending signal. PID: 5671 SIG: 9
device not found

常规模式 - 我想要获取connectionSettings活动中建立的值

package killianmills.pycast;

import android.app.Activity;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class GeneralMode extends Activity {
    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private Button buttonLeft;
    private Button buttonRight;
    private Button buttonEnter;

    private Button keyRight;
    private Button keyLeft;
    private Button keyUp;
    private Button keyDown;


    private String message;


    private static final String DEBUG_TAG = "Velocity";
    private VelocityTracker mVelocityTracker = null;

    public boolean onTouchEvent(MotionEvent event) {

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int trackPadWidth = size.x;
        int trackPadHeight = (size.y / 2)+((int)(size.y*.05));

        int touchedX = (int) event.getX();
        int touchedY = (int) event.getY();

        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        if(touchedY<trackPadHeight && touchedX < trackPadWidth ){

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if (mVelocityTracker == null) {

                        mVelocityTracker = VelocityTracker.obtain();
                    } else {

                        mVelocityTracker.clear();

                    }

                    mVelocityTracker.addMovement(event);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mVelocityTracker.addMovement(event);

                    mVelocityTracker.computeCurrentVelocity(1000);

                    message = ("0" + (int) (mVelocityTracker.getXVelocity() * .0125) + " " + (int) (mVelocityTracker.getYVelocity() * .0125)).toString();

                    SendMessage sendMessageTask = new SendMessage();
                    sendMessageTask.execute();

                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:

                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                    break;
            }
            return true;
        }
        return false;
    }

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

        textField = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        buttonLeft= (Button) findViewById(R.id.buttonLeftClick);
        buttonRight= (Button) findViewById(R.id.buttonRightClick);
        buttonEnter= (Button) findViewById(R.id.buttonEnterClick);

        keyRight= (Button) findViewById(R.id.buttonRightk);
        keyLeft= (Button) findViewById(R.id.buttonLeftk);
        keyUp= (Button) findViewById(R.id.buttonUpk);
        keyDown= (Button) findViewById(R.id.buttonDownk);


        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message = "2"+textField.getText().toString();
                textField.setText("");
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });

        buttonLeft.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="11";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });

        buttonRight.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="12";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });

        buttonEnter.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="3";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });
        keyRight.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="6";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });
        keyLeft.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="7";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });
        keyUp.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="4";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });
        keyDown.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                message="5";
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });

    }

    private class SendMessage extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                //client = new Socket("136.206.213.38", 4444);
                client = new Socket("192.168.1.39", 4444);

                printwriter = new PrintWriter(client.getOutputStream(), true);

                printwriter.write(message);

                printwriter.flush();
                printwriter.close();
                client.close(); // closing the connection

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_general_mode, menu);
        return true;
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="killianmills.pycast" >
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".GeneralMode"
            android:label="@string/title_activity_general_mode" >
        </activity>
        <activity
            android:name=".ConnectionSettings"
            android:label="@string/title_activity_connection_settings" >
        </activity>
        <activity
            android:name=".About"
            android:label="@string/title_activity_about" >
        </activity>
        <activity
            android:name=".PresentationMode"
            android:label="@string/title_activity_presentation_mode" >
        </activity>
    </application>

</manifest>

和connectionSettings

package killianmills.pycast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class ConnectionSettings extends Activity {

    private Button connectButton;
    private EditText ip, port;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connection_settings);

        ip = (EditText)findViewById(R.id.ipAddress);
        port = (EditText)findViewById(R.id.portNumber);

        addListenerOnSave();
    }

    public void addListenerOnSave() {

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

        connectButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // takes values from user input
                String holder1 = ip.getText().toString();
                String holder2 = port.getText().toString();

                Intent i = new Intent(getApplicationContext(), GeneralMode.class);
                i.putExtra("first",holder1);
                i.putExtra("second",holder2);

            }

        });

    }

}

1 个答案:

答案 0 :(得分:0)

我找到了我刚才找到的答案。

我真的希望能够在不同的活动中共享变量,而不是以线性方式将它们相互传递,解决方法是使用共享首选项。

链接:http://developer.android.com/reference/android/content/SharedPreferences.html

共享首选项允许您将值保存到xml文件中,您可以设置自己对哪些应用程序可以访问它们的权限,例如:private表示只有调用它的应用程序才能访问存储在其中的变量。这使我能够在我的应用程序中保存IP地址,端口号和鼠标灵敏度设置,并允许它们用于多种活动,例如:通过速度跟踪器发送URL和发送鼠标移动。

如果您确实想要线性发送它们,可以使用putExtra方法或使用Bundle:http://developer.android.com/reference/android/os/Bundle.html 为了在不保存的情况下将多个值发送到下一个活动,以防您不想保存每个半重要值。

我很抱歉这个问题有点笨拙,我当时是Android新手。