我环顾四周看其他问题,到目前为止还没有任何效果。当我注释掉onclicklistener及其onClick的支架时,我的代码运行正常。这是我评论的代码。
// aButton.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// startActivity(new Intent (LoginActivity.this, MapActivity.class));
// }});
这是我的LoginActivity(main)的完整代码:
package com.JTInc.tag;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.util.Log;
public class LoginActivity extends ActionBarActivity {
//Button aButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button aButton = (Button) this.findViewById(R.id.Button03);
aButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent (LoginActivity.this, MapActivity.class));
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login,
container, false);
return rootView;
}
}
}
MapActivity代码:
package com.JTInc.tag;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MapActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container,
false);
return rootView;
}
}
}
我对LogCat不是很了解,但这是我在错误类别下看到的:
05-06 12:49:12.209: E/ActivityThread(645): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05748 that was originally bound here
05-06 12:49:12.209: E/ActivityThread(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05748 that was originally bound here
05-06 12:49:12.209: E/ActivityThread(645): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-06 12:49:12.209: E/ActivityThread(645): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-06 12:49:12.209: E/ActivityThread(645): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-06 12:49:12.209: E/ActivityThread(645): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-06 12:49:12.209: E/ActivityThread(645): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-06 12:49:12.209: E/ActivityThread(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-06 12:49:12.209: E/ActivityThread(645): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-06 12:49:12.209: E/ActivityThread(645): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-06 12:49:12.209: E/ActivityThread(645): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-06 12:49:12.209: E/ActivityThread(645): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-06 12:49:12.209: E/ActivityThread(645): at java.lang.Thread.run(Thread.java:856)
05-06 12:49:12.230: E/StrictMode(645): null
05-06 12:49:12.230: E/StrictMode(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05748 that was originally bound here
05-06 12:49:12.230: E/StrictMode(645): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-06 12:49:12.230: E/StrictMode(645): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-06 12:49:12.230: E/StrictMode(645): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-06 12:49:12.230: E/StrictMode(645): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-06 12:49:12.230: E/StrictMode(645): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-06 12:49:12.230: E/StrictMode(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-06 12:49:12.230: E/StrictMode(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-06 12:49:12.230: E/StrictMode(645): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-06 12:49:12.230: E/StrictMode(645): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-06 12:49:12.230: E/StrictMode(645): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-06 12:49:12.230: E/StrictMode(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-06 12:49:12.230: E/StrictMode(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-06 12:49:12.230: E/StrictMode(645): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-06 12:49:12.230: E/StrictMode(645): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-06 12:49:12.230: E/StrictMode(645): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-06 12:49:12.230: E/StrictMode(645): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-06 12:49:12.230: E/StrictMode(645): at java.lang.Thread.run(Thread.java:856)
05-06 12:49:12.230: W/ActivityManager(294): Unbind failed: could not find connection for android.os.BinderProxy@40f5dc98
05-06 12:49:12.259: E/ActivityThread(645): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05270 that was originally bound here
05-06 12:49:12.259: E/ActivityThread(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05270 that was originally bound here
05-06 12:49:12.259: E/ActivityThread(645): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-06 12:49:12.259: E/ActivityThread(645): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-06 12:49:12.259: E/ActivityThread(645): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-06 12:49:12.259: E/ActivityThread(645): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-06 12:49:12.259: E/ActivityThread(645): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-06 12:49:12.259: E/ActivityThread(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-06 12:49:12.259: E/ActivityThread(645): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-06 12:49:12.259: E/ActivityThread(645): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-06 12:49:12.259: E/ActivityThread(645): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-06 12:49:12.259: E/ActivityThread(645): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-06 12:49:12.259: E/ActivityThread(645): at java.lang.Thread.run(Thread.java:856)
05-06 12:49:12.279: E/StrictMode(645): null
05-06 12:49:12.279: E/StrictMode(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d05270 that was originally bound here
05-06 12:49:12.279: E/StrictMode(645): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-06 12:49:12.279: E/StrictMode(645): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-06 12:49:12.279: E/StrictMode(645): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-06 12:49:12.279: E/StrictMode(645): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-06 12:49:12.279: E/StrictMode(645): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-06 12:49:12.279: E/StrictMode(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-06 12:49:12.279: E/StrictMode(645): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-06 12:49:12.279: E/StrictMode(645): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
05-06 12:49:12.279: E/StrictMode(645): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
05-06 12:49:12.279: E/StrictMode(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-06 12:49:12.279: E/StrictMode(645): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-06 12:49:12.279: E/StrictMode(645): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-06 12:49:12.279: E/StrictMode(645): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-06 12:49:12.279: E/StrictMode(645): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-06 12:49:12.279: E/StrictMode(645): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-06 12:49:12.279: E/StrictMode(645): at java.lang.Thread.run(Thread.java:856)
05-06 12:49:12.279: W/ActivityManager(294): Unbind failed: could not find connection for android.os.BinderProxy@40fd6850
05-06 12:49:21.850: E/ThrottleService(294): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
如果我没有正确地做到这一点,请告诉我。 非常感谢所有帮助。谢谢!
答案 0 :(得分:2)
首先初始化按钮,然后写下if(savedInstanceState==null)
条件。
试试如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button aButton = (Button)findViewById(R.id.Button03);
aButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent (LoginActivity.this, MapActivity.class));
}});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}