b = BitmapFactory.decodeResource(this.getApplicationContext().getResources(),R.drawable.elon);
我正在尝试创建一个照片效果应用程序,当我使用此创建位图时,这会返回空指针异常。
我所做的是创建了一个简单的活动,底部有回收视图(水平),回收者视图上方有一个图像视图。
回收者视图只有每个效果必须更改的按钮,添加了一个按钮。当从循环器视图中单击一个按钮时,主类中的方法被调用并且必须运行,我的MainActivity.java是:
公共类MainActivity扩展了AppCompatActivity {
static {
System.loadLibrary("NativeImageProcessor");
}
Toolbar tl;
ImageView mainImage;
RecyclerView rec;
ArrayList<String> list = new ArrayList<String>();
com.zomato.photofilters.imageprocessors.Filter myFilter;
Drawable b;
Bitmap input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (Toolbar)findViewById(R.id.toolbar);
tl.setTitle("Image Effects");
setSupportActionBar(tl);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
mainImage = (ImageView)findViewById(R.id.mainImage);
mainImage.setMinimumHeight((int) (0.6 * height));
rec= (RecyclerView)findViewById(R.id.rec);
rec.setLayoutManager(new
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
list.add("Keep original");
list.add("Star Lit Filter");
list.add("Blue Mess Filter");
list.add("Awe Stuck Vibe Filter");
list.add("Lime Stutter Filter");
list.add("Night Whispher Filter");
HorizontalAdapter ha=new HorizontalAdapter(list);
rec.setAdapter(ha);
}
public void imageOutput(int filterNum){
try{
input = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.mipmap.ic_launcher_round);
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
switch (filterNum){
case 0:
mainImage.setImageBitmap(input);
break;
case 1:
myFilter = SampleFilters.getStarLitFilter();
mainImage.setImageBitmap(myFilter.processFilter(input));
break;
case 2:
myFilter = SampleFilters.getBlueMessFilter();
mainImage.setImageBitmap(myFilter.processFilter(input));
break;
case 3:
myFilter = SampleFilters.getAweStruckVibeFilter();
mainImage.setImageBitmap(myFilter.processFilter(input));
break;
case 4:
myFilter = SampleFilters.getLimeStutterFilter();
mainImage.setImageBitmap(myFilter.processFilter(input));
break;
case 5:
myFilter = SampleFilters.getNightWhisperFilter();
mainImage.setImageBitmap(myFilter.processFilter(input));
break;
}
}
}
这是我的HorizontalAdapter.java类:
public class HorizontalAdapter extends
RecyclerView.Adapter<HorizontalAdapter.myViewHolder> {
ArrayList<String> list;
MainActivity ma = new MainActivity();
HorizontalAdapter(ArrayList list){
this.list = list;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new myViewHolder(v);
}
@Override
public void onBindViewHolder(myViewHolder holder, final int position) {
holder.filterButton.setText(list.get(position));
holder.filterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("exe","1");
ma.imageOutput(position);
Log.i("exe","0");
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class myViewHolder extends RecyclerView.ViewHolder{
Button filterButton;
public myViewHolder(View itemView) {
super(itemView);
filterButton = (Button)itemView.findViewById(R.id.filterButton);
}
}
}
此处从水平回收器适配器调用ImageOutput方法,将按钮的位置作为参数传递。 我得到的错误是在try catch块中的位图,显示错误为空对象引用和崩溃,我在监视器中的错误程序是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ag.testphotofilterwithbuttons, PID: 2528
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.ag.testphotofilterwithbuttons.MainActivity.imageOutput(MainActivity.java:69)
at com.example.ag.testphotofilterwithbuttons.HorizontalAdapter$1.onClick(HorizontalAdapter.java:38)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
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)
答案 0 :(得分:0)
改为使用ContextCompat
ContextCompat.getDrawable(context, R.drawable.elon);
答案 1 :(得分:0)
MainActivity ma = new MainActivity();
从不自己创建活动的实例。
由于MainActivity
是使用HorizontalAdapter
的人,因此请将HorizontalAdapter
的构造函数参数添加到MainActivity
:
MainActivity ma;
HorizontalAdapter(MainActivity ma, ArrayList list){
this.list = list;
this.ma = ma;
}
MainActivity
可以传递this
:
HorizontalAdapter ha=new HorizontalAdapter(this, list);
rec.setAdapter(ha);