我使用线程和持有者下载一些数据,然后显示一个带有按钮的新视图,您可以通过该应用程序进一步浏览。 但我认为我做错了,因为我使用的任何下一个活动都会在setContentView行上遇到空指针异常崩溃。当我评论这一行时,没有任何问题(所以我想这意味着活动在清单中被宣布为良好并正常启动)。 看起来好像是因为线程新活动没有地方设置内容或什么..
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.hera.ontdekdelft.model.JJsonResponse;
import com.hera.ontdekdelft.listclasses.ListData;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class StartUp extends Activity
{
private static final String tag = StartUp.class.getName();
public static final String SERIALIZEDDATA = "SerializedData";
ProgressBar pbar;
TextView tv1;
Drawable background;
Button btCategory, btMakeMyDay;
RelativeLayout loadedBackground, loadingBackground;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LoadData task = new LoadData();
task.execute();
String language = Locale.getDefault().getISO3Language();
background = getBackgroundImage();
setContentView(R.layout.startup_loading);
tv1=(TextView)findViewById(R.id.tvloading);
if(language.equals("nld")){
tv1.setText("bijwerken");
}else{
tv1.setText("loading");
}
pbar=(ProgressBar)findViewById(R.id.progressBar1);
pbar.setVisibility(1);
loadingBackground=(RelativeLayout)findViewById(R.id.startup_background_loading);
loadingBackground.setBackgroundDrawable(background);
}
public Drawable getBackgroundImage(){
Drawable d;
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
Log.i("month",String.valueOf(month));
switch (month){
default: d = getResources().getDrawable(R.drawable.homescreen_summer);break;
case 0:d = getResources().getDrawable(R.drawable.homescreen_winter);break;
case 1:d = getResources().getDrawable(R.drawable.homescreen_winter); break;
case 2:d = getResources().getDrawable(R.drawable.homescreen_spring);break;
case 3:d = getResources().getDrawable(R.drawable.homescreen_spring);break;
case 4:d = getResources().getDrawable(R.drawable.homescreen_spring);break;
case 5:d = getResources().getDrawable(R.drawable.homescreen_summer); break;
case 6:d = getResources().getDrawable(R.drawable.homescreen_summer); break;
case 7:d = getResources().getDrawable(R.drawable.homescreen_summer); break;
case 8:d = getResources().getDrawable(R.drawable.homescreen_autumn); break;
case 9:d = getResources().getDrawable(R.drawable.homescreen_autumn); break;
case 10:d = getResources().getDrawable(R.drawable.homescreen_autumn); break;
case 11:d = getResources().getDrawable(R.drawable.homescreen_winter); break;
}
return d;
}
private class LoadData extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected void onPostExecute(String language) {
super.onPostExecute(language);
setContentView(R.layout.startup_loaded);
btCategory=(Button)findViewById(R.id.btn_category);
loadedBackground=(RelativeLayout)findViewById(R.id.startup_background_loaded);
loadedBackground.setBackgroundDrawable(background);
if(language.equals("nld")){
btCategory.setText("Ontdek Delft");
}else{
btCategory.setText("Discover Delft");
}
//newactivitybutton
btCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(StartUp.this, Overview.class));
finish();
}
});
}
@Override
protected String doInBackground(Void... params) {
String language = Locale.getDefault().getISO3Language();
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("originalDelftJson.json");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
ObjectMapper objectMapper = new ObjectMapper();
Log.i("tijdlog","start parsing" );
try {
List<JJsonResponse> jsonResponse = objectMapper.readValue(inputStream, new TypeReference<List<JJsonResponse>>() { });
Log.i("tijdlog","einde parsing" );
//
final List<JJsonResponse> myGlobalVariable = jsonResponse;
((ApplicationController)getApplication()).setGlobalData(myGlobalVariable);
List<ListData> ld = new ArrayList<ListData>();
int selectedPic;
JJsonResponse e;
int k =0;
for(int i=0;i < jsonResponse.size() ;i++){
e = jsonResponse.get(i);
for(int j=0; j<e.venue.themes.size();j++){
if (e.venue.themes.get(j).mobile == true){
selectedPic = 0;
String tip;
String theme;
if (language.equalsIgnoreCase("nld")){
tip=e.venue.tip;
theme=e.venue.themes.get(j).name;
}else{
tip=e.venue.tip_en;
theme=e.venue.themes.get(j).name_en;
}
// pic selected pic
String photoUrl = null;
if (e.venue.venue_photos.isEmpty() == false){
for(int l=0; l < e.venue.venue_photos.size() ;l++){
if(e.venue.venue_photos.get(l).selected == true){
selectedPic = l;
}
photoUrl=e.venue.venue_photos.get(selectedPic).medium;
}
}else
{
photoUrl=null; // null no pic--> add link later
}
ld.add(new ListData(e.venue.id, e.venue.name, photoUrl, tip, theme));//nieuwe maken
k++;
}
}
}
Collections.sort(ld, ListData.ListDataThemeAndNameComparator); // vergelijken op thema
for(int z=0;z < ld.size() ;z++){
Log.i("ld nr " + String.valueOf(z), ld.get(z).name + " theme: " + ld.get(z).theme + " & venue id= "+ ld.get(z).venueID);
}
// save
((ApplicationController)getApplication()).setListData(ld);
} catch (JsonParseException e) {
// XXX Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// XXX Auto-generated catch block
e.printStackTrace();
}
return language;
}
}
}
发生错误的新活动:
import android.app.Activity; import android.os.Bundle;
公共类概述扩展了活动{
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(findViewById(R.layout.splash)); }
}
xml splash;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/splash"
>
</LinearLayout>
修改 我编辑了我的代码。现在使用AsyncTask,它似乎是像下面提出的LuxuryMode解决方案,但它也不起作用。当setContentView时,仍然是相同的nullpointerexception ... 错误日志:
07-24 19:53:50.411: E/AndroidRuntime(29618): FATAL EXCEPTION: main
07-24 19:53:50.411: E/AndroidRuntime(29618): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hera.ontdekdelft/com.hera.ontdekdelft.Overview}: java.lang.NullPointerException
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.os.Looper.loop(Looper.java:144)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread.main(ActivityThread.java:4937)
07-24 19:53:50.411: E/AndroidRuntime(29618): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:53:50.411: E/AndroidRuntime(29618): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 19:53:50.411: E/AndroidRuntime(29618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-24 19:53:50.411: E/AndroidRuntime(29618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-24 19:53:50.411: E/AndroidRuntime(29618): at dalvik.system.NativeStart.main(Native Method)
07-24 19:53:50.411: E/AndroidRuntime(29618): Caused by: java.lang.NullPointerException
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.view.ViewGroup.addViewInner(ViewGroup.java:1977)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.view.ViewGroup.addView(ViewGroup.java:1873)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.view.ViewGroup.addView(ViewGroup.java:1853)
07-24 19:53:50.411: E/AndroidRuntime(29618): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:226)
07-24 19:53:50.411: E/AndroidRuntime(29618): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:216)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.Activity.setContentView(Activity.java:1665)
07-24 19:53:50.411: E/AndroidRuntime(29618): at com.hera.ontdekdelft.Overview.onCreate(Overview.java:12)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
07-24 19:53:50.411: E/AndroidRuntime(29618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
07-24 19:53:50.411: E/AndroidRuntime(29618): ... 11 more
答案 0 :(得分:1)
Android线程模型中有两个规则: 1)不要阻止主(UI)线程2)不要在主(UI)线程之外操作UI。你似乎是遇到第二条规则的问题。您可以尝试使用Activity#runOnUiThread(Runnable)而不是使用该处理程序,但一般来说,您正在做的事情非常混乱,通常是一个糟糕的想法:)另外,除非我遗漏了什么,你的处理程序究竟是什么如果你在启动线程后初始化它,可以在你的启动线程中使用?你似乎依赖于某种方式的线程调度,这也是一个坏主意。
您很可能需要使用的是AsyncTask
。立即在onCreate中设置您的内容视图,然后执行下载数据的AsyncTask
。然后在任务的onPostExecute中,执行必要的相关UI更改。
更新 -
在onPostExecute中,立即调用super。此外,请勿通过致电startActivity(new Intent ("com.hera.ontdekdelft.OVERVIEW"));
来启动您的活动,而应该是startActivity(new Intent(Startup.this, Overview.class));
此外,从堆栈跟踪的外观来看,当您尝试启动新活动时发生崩溃。所以我会在12号线上看看问题是什么。
更新 -
正如我在下面的讨论中指出的那样:
您无法将Activity的contentView设置为findViewById(无论如何)。 findViewById查看当前的contentView,并尝试查找与您传入的id匹配的视图。但是您还没有设置任何内容视图,因此无需查找。因此,当您尝试将contentview设置为调用findViewById返回的null视图时,您会使用nullpointer崩溃!您需要将视图设置为该Activity的视图。 setContentView(R.layout.overview_layout)或setContentView(new OverviewView(this))
答案 1 :(得分:0)
setContentView(..)是Activity的方法。你无法在Runnable
中调用它,它超出了范围。结果是空指针。
你在Runnable中设置contentView的方法相当奇怪..也许改变你的设计一点?也许你可以创建多个Activity而不是切换1个Activity的布局..