我正在创建一个应该显示公交路线的应用程序 我的问题是在显示路线时出现NullpointerException 使用Lyoutinflater创建动态布局 我从sqlite3数据库获取数据并用光标过滤掉它 我试着在5个textViews的布局中显示它 我认为在TextViews中创建布局和/或显示数据是失败的。
这是我的Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.PlanAusgabeSubActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at at.atn.android.PlanAusgabeSubActivity.getRoute(PlanAusgabeSubActivity.java:111)
at at.atn.android.PlanAusgabeSubActivity.onCreate(PlanAusgabeSubActivity.java:65)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
这是我的计划:
public void getRoute() {
db = myDbHelper.getReadableDatabase();
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
txtAbfahrt = (TextView)findViewById(R.id.txZeit);
txtDauer = (TextView)findViewById(R.id.txDauer);
txtRoute = (TextView)findViewById(R.id.txUmstieg);
txtUmstiege = (TextView)findViewById(R.id.txRoute);
db = myDbHelper.getReadableDatabase();
// Alle Daten der Datenbank abrufen mithilfe eines Cursors
Cursor cursor = db.rawQuery("SELECT \tstrftime('%H:%M', f.abfahrt) AS Abfahrt,\n" +
"\tstrftime('%H:%M', f.ankunft) AS Ankunft,\n" +
"\tstrftime('%H:%M', strftime('%s',f.ankunft)- strftime('%s',f.abfahrt), 'unixepoch') AS Dauer,\n" +
"\tr.name AS Route,\n" +
"\tcount(u.fahrt_id) AS Umstiege\n" +
"FROM \tscotty_fahrt f\n" +
"JOIN \tscotty_haltestelle start \tON f.start_id \t= start.id\n" +
"JOIN \tscotty_haltestelle ziel \tON f.ziel_id \t= ziel.id\n" +
"JOIN \tscotty_route r \t\t\tON f.route_id \t= r.id\n" +
"LEFT OUTER JOIN scotty_umstiegsstelle u\t\t ON f.id = u.fahrt_id\n" +
"WHERE \tstart.name = 'Linz/Donau Hbf (Busterminal)'\n" +
"AND \tziel.name = 'Neufelden Busterminal (Schulzentrum)'\n" +
"GROUP BY u.fahrt_id",null);
cursor.moveToFirst();
while (cursor.moveToNext()){
//in this string we get the record for each row from the column "name"
int i =0;
View v = inflater.inflate(R.layout.layoutausgabe,null);
View insertPoint = findViewById(R.id.layout);
String abfahrtszeit = cursor.getString(0);
String dauer = cursor.getString(1);
String route = cursor.getString(2);
String umstiege = cursor.getString(3);
((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT));
txtAbfahrt.setText(abfahrtszeit);
txtDauer.setText(dauer);
txtRoute.setText(route);
txtUmstiege.setText(umstiege);
i++;
}
//here we close the cursor because we do not longer need it
//}
cursor.close();
myDbHelper.close();
}
答案 0 :(得分:0)
请使用
inflater.findViewById(R.id.txZeit);
而不是
findViewById(R.id.txZeit);
答案 1 :(得分:0)
我认为你不会夸大你的布局。请充气:
view = inflater.inflate(R.layout.sub_layout, null, false);
然后使用此
访问您的组件 txtAbfahrt = (TextView)view .findViewById(R.id.txZeit);
txtDauer = (TextView)view .findViewById(R.id.txDauer);
txtRoute = (TextView)view .findViewById(R.id.txUmstieg);
txtUmstiege = (TextView)view .findViewById(R.id.txRoute);
答案 2 :(得分:0)
我认为你的textViews txtAbfahrt,txtDauer,txtRoute和txtUmstiege都包含在你的layoutausgabe中。
你需要在给layoutausgabe充气后找到它们。你需要在v
变量中找到它们。
while (cursor.moveToNext()){
//in this string we get the record for each row from the column "name"
int i =0;
View v = inflater.inflate(R.layout.layoutausgabe,null);
View insertPoint = findViewById(R.id.layout);
String abfahrtszeit = cursor.getString(0);
String dauer = cursor.getString(1);
String route = cursor.getString(2);
String umstiege = cursor.getString(3);
txtAbfahrt = (TextView)v.findViewById(R.id.txZeit);
txtDauer = (TextView)v.findViewById(R.id.txDauer);
txtRoute = (TextView)v.findViewById(R.id.txUmstieg);
txtUmstiege = (TextView)v.findViewById(R.id.txRoute);
((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT));
txtAbfahrt.setText(abfahrtszeit);
txtDauer.setText(dauer);
txtRoute.setText(route);
txtUmstiege.setText(umstiege);
i++;
}
答案 3 :(得分:0)
{
// some code here
View v = inflater.inflate(R.layout.layoutausgabe,null);
// then find view by its id
}
首先夸大布局,然后找到相关的视图