我的应用程序已经“遗憾地已停止工作”但在调试中似乎没有错误。到目前为止,它只是一些按钮和带有一个按钮的文本,我试图连接到一个新的活动。 这是控制台正在运行,
Waiting for device.
Target device: Nexus_6_API_22 [emulator-5554]
Uploading file
local path: C:\Users\CALLUM\AndroidStudioProjects\Switch-game2\app\build\outputs\apk\app-debug.apk
remote path: /data/local/tmp/gameswitch.switch_game
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop gameswitch.switch_game
Launching application: gameswitch.switch_game/gameswitch.switch_game.MainActivity.
DEVICE SHELL COMMAND: am start -D -n "gameswitch.switch_game/gameswitch.switch_game.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=gameswitch.switch_game/.MainActivity }
Connected to the target VM, address: 'localhost:8610', transport: 'socket'
Disconnected from the target VM, address: 'localhost:8610', transport: 'socket'
这是我的代码,
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/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>
//new below here2
<activity
android:name=".GamePlay"
android:label="@string/app_name">
<intent-filter>
<action
android:name="in.wptrafficanalyzer.AnotherActivity"/>
<category
android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
和下一部分
package gameswitch.switch_game;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new below
//Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Retro.ttf");
//TextView myTextView = (TextView)findViewById(R.id.title);
//myTextView.setTypeface(myTypeface);
//new below 2
OnClickListener listnr = new OnClickListener(){
@Override
public void onClick(View v){
Intent i = new Intent("GamePlay");
startActivity(i);
}
};
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(listnr);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
其他类和co是标准字符串和东西。 我对Android Studio中的编程很新,我真的不确定是什么,因为我没有看到下划线或错误。
由于再次运行,出现的错误如下
Process: gameswitch.switch_game, PID: 2774
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=GamePlay }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1781)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at gameswitch.switch_game.MainActivity$1.onClick(MainActivity.java:32)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
你的意图是错的。 您应该明确设置活动
Intent intent = new Intent(this, GamePlay.class);
或使用您在清单中声明的操作
Intent intent = new Intent();
intent.setAction("in.wptrafficanalyzer.AnotherActivity");
您的代码失败,因为您启动了未声明的操作意图。