以前我用android使用sdk 17做了一些代码,其中活动扩展了Activity。但昨天我更新了我的sdk经理。我面临的第一个问题是我无法使用min-sdk构建应用程序,而不是API-14。所以,我使用了min sdk-14并继续进行。当我在main_fragmet中给出按钮,textview等时 - 应用程序正在运行。但是当我试图通过mainActivity中的代码赋予textview值时会出现问题。以下是代码:
public class MainActivity extends Activity {
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.txtvw);
t.setText("Shaonn");
//t.setText("Shaon");
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
在AVD中它没有运行且错误显示不幸的是应用程序已停止运行。
来自logcat的错误:
05-15 06:46:21.812: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-15 06:46:23.382: E/ActivityThread(555): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd5418 that was originally bound here
05-15 06:46:23.382: E/ActivityThread(555): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd5418 that was originally bound here
05-15 06:46:23.382: E/ActivityThread(555): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-15 06:46:23.382: E/ActivityThread(555): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-15 06:46:23.382: E/ActivityThread(555): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-15 06:46:23.382: E/ActivityThread(555): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-15 06:46:23.382: E/ActivityThread(555): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-15 06:46:23.382: E/ActivityThread(555): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-15 06:46:23.382: E/ActivityThread(555): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-15 06:46:23.382: E/ActivityThread(555): at java.util.concurrent.FutureTask.run (FutureTask.java:234)
05-15 06:46:23.382: E/ActivityThread(555): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-15 06:46:23.382: E/ActivityThread(555): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-15 06:46:23.382: E/ActivityThread(555): at java.lang.Thread.run(Thread.java:856)
05-15 06:46:23.412: E/StrictMode(555): null
答案 0 :(得分:2)
错误:
Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd5418 that was originally bound here
05-15 06:46:23.382: E/ActivityThread(555): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd5418 that was originally bound here
尝试在“模拟器”选项中禁用主机GPU,它将解决问题。
同时强>
如果您在TextView
文件中fragment.xml
,则无法访问TextView
中的Mainactivity
。我应该在placefragment文件中。
public class PlaceholderFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
TextView t=(TextView) v.findViewById(R.id.txtvw);
t.setText("Shaonn");
return v;
}
}
答案 1 :(得分:0)
这就是问题...你在fragment.xml中有你的textview但是你在activity_main.xml中初始化它..从这里删除代码
并将其粘贴到片段
中t=(TextView) rootView.findViewById(R.id.txtvw);
t.setText("Shaonn");