Android 용으로 매우 익숙합니다. 샘플 프로젝트를 만들었고 토스트를 표시 할 수 있습니다. 이제 일부 데이터를 표에 표시하려고합니다.프로그래밍 방식으로 TableLayout을 만드는 Android
내에서 onCreate 방법 (여기에 전체 방법)이 시도했다 : 때
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLay"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/section_label"
android:layout_centerHorizontal="true"
android:text="Welcome!"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableLayout
android:id="@+id/MyLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/section_label"
android:layout_marginLeft="18dp"
android:layout_marginTop="40dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
문제는 여기
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
try {
TableLayout table = (TableLayout) findViewById(R.id.navigation_drawer);
TableRow tr = new TableRow(this);
TextView textview = new TextView(this);
textview.setText("Sup");
textview.setTextColor(Color.YELLOW);
tr.addView(textview);
table.addView(tr);
Toast.makeText(getApplicationContext(), "Well Done on getting the data", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
그리고 내 fragment_main.xml 파일입니다 앱을 실행하면 다음과 같이 표시됩니다.
java.lang.NullPointerException
at com.example.helloworld.MainActivity.onCreate(MainActivity.java:115)
at android.app.Activity.performCreate(Activity.java:5431)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
이것은 115 행입니다 :
table.addView(tr);
왜 테이블이 null입니까? 간단한 테이블을 표시하는 것이 왜 그렇게 어려운가요? 도와주세요 ..
작업 또는 작업중인 조각입니까? – denizt
onCreate 메서드 표시 – Suvitruf
setContentView 메서드를 확인하십시오. 이 레이아웃에는 TableLayout (MyLayout1)이 포함되어야합니다. – Amsheer