我有一个gridView,我想用我的字符串填充。但是在找到要在其中打印特定字符串的TextView时出现问题。
我使用android:tag
以XML格式分配了标签,现在使用标签查找相应的TextView。但是当我setText()
时,应用程序崩溃,发出NullPointerException。
这是代码
public class OneDayTimeTable extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_day_layout);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
View view = new View(OneDayTimeTable.this);
TextView tv = (TextView)view.findViewWithTag("mon_11.30");
tv.setText("Hi");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/class_textView"
android:text="Class: "
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:textAppearance="?android:textAppearanceMedium"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/section_textView"
android:textAppearance="?android:textAppearanceMedium"
android:text="Section: "
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_below="@+id/class_textView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/date_textView"
android:text="Date: "
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:textAppearance="?android:textAppearanceMedium"
android:layout_below="@+id/section_textView"
android:layout_centerHorizontal="true"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_below="@id/date_textView"
android:id="@+id/timetable_scroll_view">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="9"
android:rowCount="6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="/"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="10:30AM-11:30AM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="11:30AM-12:15PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="12:15PM-1:15PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="1:15PM-2:00PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="2:00PM-3:00PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="3:00PM-4:00PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="4:00PM-5:00PM"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="0"
android:layout_column="8" />
<TextView
android:id="@+id/moday_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Monday"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:id="@+id/monday_10.30"
android:tag="mon_10.30"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:id="@+id/monday_11.30"
android:tag="mon_11.30"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="L"
android:textAppearance="?android:textAppearanceLarge"
android:layout_row="1"
android:layout_column="4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:tag="mon_01.15"
android:id="@+id/monday_1.15"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:tag="mon_02.00"
android:id="@+id/monday_2.00"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="6" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:tag="mon_03.00"
android:id="@+id/monday_3.00"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_margin="5dp"
android:layout_rowWeight="1"
android:gravity="center"
android:text="Free"
android:tag="mon_04.00"
android:id="@+id/monday_4.00"
android:textAppearance="?android:textAppearanceMedium"
android:layout_row="1"
android:layout_column="8" />
</GridLayout>
</HorizontalScrollView>
</RelativeLayout>
错误日志:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tonystark.adminlogin/com.example.tonystark.adminlogin.OneDayTimeTable}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tonystark.adminlogin.OneDayTimeTable.onCreate(OneDayTimeTable.java:37)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
string(5) "http:"
您正在创建一个空视图并尝试在其中查找视图:)
答案 1 :(得分:0)
setContentView(R.layout.one_day_layout)之后;您可以使用以下行
直接获取TextViewTextView tv = (TextView) findViewById(R.id.monday_11.30);
现在,这应该有效:
tv.setText("Hi");