Android onCreate函数不起作用
我已经开发了一个简单的android应用程序,将图像加载到互联网的url上,并在imageview中显示给用户(仅此而已),当我将所有代码放在onCreate函数中时,应用程序不工作,而将相同的代码放在onStart中,应用程序工作并显示图像。。。。 我以前还有一个类似的问题:当我想在应用程序启动时(使用onCreate函数)更改textview文本属性时,我失败了,应用程序崩溃了,但在onStart中使用相同的代码解决了问题,请帮助我,我遇到了这个该死的问题。 你能解释一下为什么会发生这种情况吗? 我的密码Android onCreate函数不起作用,android,Android,我已经开发了一个简单的android应用程序,将图像加载到互联网的url上,并在imageview中显示给用户(仅此而已),当我将所有代码放在onCreate函数中时,应用程序不工作,而将相同的代码放在onStart中,应用程序工作并显示图像。。。。 我以前还有一个类似的问题:当我想在应用程序启动时(使用onCreate函数)更改textview文本属性时,我失败了,应用程序崩溃了,但在onStart中使用相同的代码解决了问题,请帮助我,我遇到了这个该死的问题。 你能解释一下为什么会发生这种情况
通过一些猜测,
onCreate()
和onStart()
之间的区别:您的imageView1
不在活动布局中,而是在片段布局中。挂起的片段事务在super.onStart()
中执行,片段将附加到活动。在此之后,在活动视图层次结构上调用findViewById()
实际上会找到视图,并且不会像在onCreate()
阶段那样返回null
除此之外,您正在主线程上执行网络操作(您不应该这样做),您应该受到networkMainThreadException
的惩罚,除非您使用StrictMode
或targetSdkVersion
将其抑制在11以下
您还吞下了任何
异常
,这些异常阻止您了解try
块中的任何问题。考虑至少记录异常StActTrace.< /P>我的问题是当应用程序启动时,在OnCuto中,我不能改变像TeXView或IVieview这样的视图元素,但是如果把所有的代码都放在一个函数中,我用一个按钮来调用它。或者加入onStart…是的。在生命周期的这些点上,片段事务已经运行,并且片段是活动视图层次结构的一部分。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
ImageView img=(ImageView) findViewById(R.id.imageView1);
try {
URL url=new URL("http://player.arsenal.com/site_media/images /uploaded/videothumbs/001702f8095b04cfc885ff1a79375312/552x310/idx-10.jpg");
HttpGet httpRequest=null;
httpRequest= new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (Exception ex) {
}
}
@Override
protected void onStart() {
super.onStart();
String msg = null;
Log.d(msg, "The onStart() event");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
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_main, container, false);
return rootView;
}
}
}