由意图传递或 android 中的活动更改引起的崩溃
Posted
技术标签:
【中文标题】由意图传递或 android 中的活动更改引起的崩溃【英文标题】:Crash caused by either passing of intent or changing of activity in android 【发布时间】:2015-03-12 15:43:34 【问题描述】:我不确定这个错误到底发生在哪里。
我使用 Intent 将主菜单链接到其他活动没有问题
我正在尝试为我的应用程序的连接设置获取两个值,然后将其传递给另一个活动,以便它可以连接到服务器。同样,我不完全确定出了什么问题。
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 ComponentInfokillianmills.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>
和连接设置
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);
);
【问题讨论】:
你检查过这个吗:***.com/questions/3278091/… 我把所有的类都公开了,没有 arg 构造函数,还是一样的崩溃 发布代码(至少是GeneralMode
类中的构造函数和 onCreate()
methods 并发布您的清单。
已更新,抱歉耽搁了
我自己解决了这个问题,我觉得我问的问题有点不对。我已经使用 SharedPreferences 来存储这些值,以便我的其他两个活动可以访问 IP 地址和端口号,感谢输入人员。 developer.android.com/reference/android/content/…
【参考方案1】:
我已经找到了我一直在寻找的答案。
我真的希望能够在不同活动之间共享变量,而不是以线性方式将它们相互传递,解决方案是使用共享偏好。
链接:http://developer.android.com/reference/android/content/SharedPreferences.html
共享首选项允许您将值保存到 xml 文件中,您可以设置自己的权限,哪些应用可以访问它们,例如:私有意味着只有调用它的应用才能访问存储在其中的变量。这让我可以在我的应用中保存 IP 地址、端口号和鼠标灵敏度设置,并允许它们用于多种活动,例如:发送 URL 和通过速度跟踪器发送鼠标移动。
如果您确实想以线性方式发送它们,您可以使用 putExtra 方法或使用 Bundle:http://developer.android.com/reference/android/os/Bundle.html 为了将多个值发送到下一个活动而不保存它,以防您不想保存每个半重要的值。
抱歉,我问这个问题有点尴尬,当时我是 android 新手。
【讨论】:
以上是关于由意图传递或 android 中的活动更改引起的崩溃的主要内容,如果未能解决你的问题,请参考以下文章
如何使用单击事件和意图将当前日期从一个活动传递到android中的下一个活动