嗨,我是Android的新手。我有一个XML布局文件,其中包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/items_tab_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentTop="true"
android:foregroundGravity="center"
android:layout_margin="50dp"
>
<Button
android:layout_width="24dp"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:foregroundGravity="center"
android:background="@drawable/ic_cancel_black_24dp"
android:layout_height="24dp" />
<com.arosis.pos.aropay.customization.TextViewNumericFont
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:hint="0"/>
<com.arosis.pos.aropay.customization.EditTextFont
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center"
android:lines="1"
android:maxLines="1"
android:inputType="text"
android:hint="نام کالا یا سرویس(اختیاری)"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/keys_layout"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_alignParentBottom="true">
<android.support.v7.widget.GridLayout
android:id="@+id/gridKeys"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:numColumns="4"
android:stretchMode="columnWidth"
app:useDefaultMargins="true"
app:columnCount="4"
app:rowCount="4">
<ImageView
android:id="@+id/button1"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="0.5"
android:background="@drawable/back"
android:text="1"
android:src="@drawable/ic_layers_clear_black_24dp"
android:padding="30dp"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="3"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button2"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="9"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="2"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button3"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="8"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="1"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button11"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="7"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="0"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<ImageView
android:id="@+id/button4"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="4"
android:src="@drawable/ic_backspace_black_24dp"
android:textAlignment="center"
android:padding="30dp"
android:textSize="25sp"
app:layout_column="3"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button5"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="6"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="2"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button6"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="5"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="1"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button12"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="4"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="0"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<ImageView
android:id="@+id/button13"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:textAlignment="center"
android:textSize="25sp"
android:src="@drawable/ic_add_box_black_24dp"
android:padding="78dp"
app:layout_column="3"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowSpan="2"
app:layout_rowWeight="2" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button7"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="1"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="0"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button8"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="3"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="2"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button9"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="2"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="1"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button14"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="000"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="0"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
<com.arosis.pos.aropay.customization.ButtonRezvanFont
android:id="@+id/button15"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="@drawable/back"
android:text="0"
android:textAlignment="center"
android:textSize="25sp"
app:layout_column="1"
app:layout_columnSpan="2"
app:layout_columnWeight="2"
app:layout_row="3"
app:layout_rowSpan="1"
app:layout_rowWeight="1" />
</android.support.v7.widget.GridLayout>
</RelativeLayout>
</RelativeLayout>
我想以编程方式更改“key_layout”的高度,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenUtility = new ScreenUtility(this);
RelativeLayout gridKey = (RelativeLayout) findViewById(R.id.keys_layout);
RelativeLayout.LayoutParams gridParam = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 200);
gridParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
gridKey.setLayoutParams(gridParam);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
viewDetailFragment();
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("سفارش"));
tabLayout.addTab(tabLayout.newTab().setText("فهرست"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
Typeface tabsFace = Typeface.createFromAsset(getAssets(), "fonts/iransans.ttf");
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildCount = vgTab.getChildCount();
for (int i = 0; i < tabChildCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(tabsFace);
}
}
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Screen Utility
ScreenUtility screenUtility = new ScreenUtility(this);
if (screenUtility.getDpWidth() >= 820) {
mTablet = true;
}
if (screenUtility.getDpWidth() < 820 && screenUtility.getDpHeight() < 820) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
但是当我启动应用程序时,它将被停止!当我删除setLayoutParams行时,它可以正常工作。
Process: com.arosis.pos.aropay, PID: 10773
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arosis.pos.aropay/com.arosis.pos.aropay.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5382)
at android.app.ActivityThread.access$1200(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.arosis.pos.aropay.MainActivity$override.onCreate(MainActivity.java:59)
at com.arosis.pos.aropay.MainActivity$override.access$dispatch(MainActivity.java)
at com.arosis.pos.aropay.MainActivity.onCreate(MainActivity.java:0)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5382)
at android.app.ActivityThread.access$1200(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我该怎么办?
答案 0 :(得分:0)
错误告诉您gridKey变量为null。我发现这种情况发生的唯一方法是R.layout.activity_main
不包含ID为keys_layout
的RelativeLayout。仔细检查您发布的代码不是来自中间目录。然后再做一次干净再试一次。