03-23 01:06:33.111 11547-11547/com.compscitutorials.basigarcia.navigationdrawervideotutorial E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.compscitutorials.basigarcia.navigationdrawervideotutorial, PID: 11547
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4449)
at com.forever.technology.bscit.Sem1Fragment_list.onListItemClick(Sem1Fragment_list.java:50)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
然后我需要以编程方式设置保证金:
<RelativeLayout>
...
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop=”11dp”
android:background=”@drawable/image” />
</RelativeLayout>
ImageView仍然有marginLeft 10和marginTop 11;
有什么问题?
答案 0 :(得分:0)
尝试使用ViewGroup.MarginLayoutParams为ImageView
设置边距。以下是来自@Hiren Patel answer的方法,对我来说非常适用。
private void setMargins (View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
代码中如何使用的示例:
public void testMargin(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 100;
params.height = 100;
setMargins(imageView , 0, 0, 0, 0);
imageView.setLayoutParams(params);
}
我的测试布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
tools:context="com.g2o.test.TestActivity">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:background="@android:color/holo_red_dark" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:adjustViewBounds="true"
android:background="@drawable/cast_ic_notification_0" />
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="testMargin"
android:text="Test" />" />
</RelativeLayout>
希望这会有所帮助!!
答案 1 :(得分:0)
您是否尝试制作新的LayoutParams?
ImageView imageView = (ImageView) findViewRoot(R.id.imageView1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0,0,0,0);
imageView.setLayoutParams(params);
答案 2 :(得分:0)
希望此帮助将您的ImageView放在RelativeLayout中,并将其宽度和高度设置为Match-Parent
<RelativeLayout
android:id="@+id/lay_expandable"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_expansion" />
</RelativeLayout>
,并且在您的活动中使用LinearLayout.LayoutParams
例如
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, 400);
lay_layout.setLayoutParams(params);
//请注意展开相对布局,而不是图像本身
答案 3 :(得分:0)
这对某人可能有用: 在我的情况下,我正在处理相同的问题,即在android api 19上无法识别页边距,所以我要做的是更改图像的大小(稍微增加一点),然后调整填充,如下所示:
file: dimens.xml
<dimen name="image_padding_right">5dp</dimen>
然后输入代码:
val imageView1 = ImageView(this.activity)
imageView1.setPadding(0,0,
resources.getDimension(R.dimen.image_padding_right).roundToInt(), 0)
imageView1.cropToPadding = true
使用 cropToPadding 选项可以解决此问题,以防页边空白不起作用。
希望对您有帮助。