import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.abhilasha.marvelsfacts.R;
import com.example.abhilasha.marvelsfacts.models.SuperHero;
import com.squareup.picasso.Picasso;
public class ExhibitDetailActivity extends AppCompatActivity {
public static final String EXTRA_SUPERHERO="extra_superhero";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exhibit_detail);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SuperHero superHero=getIntent().getExtras().getParcelable("EXTRA_SUPERHERO");
TextView description=(TextView)findViewById(R.id.description);
ImageView image=(ImageView)findViewById(R.id.image);
description.setText(superHero.getDescription());//getDescription has
an null point error
Picasso.with(this).load(superHero.getImage()).into(image);
}
}
这是我的ExhibitAdapter.java文件
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.abhilasha.marvelsfacts.R;
import com.example.abhilasha.marvelsfacts.models.SuperHero;
import com.squareup.picasso.Picasso;
public class ExhibitsAdapter extends ArrayAdapter<SuperHero> {
public ExhibitsAdapter(@NonNull Context context, @LayoutRes int
resource) {
super(context, resource);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup
parent) {
ViewHolder holder;
if( convertView == null ) {
holder = new ViewHolder();
convertView = LayoutInflater.from( getContext() ).inflate(
R.layout.view_exhibit_list_item, parent, false );
holder.name = (TextView) convertView.findViewById( R.id.name );
holder.thumbnail = (ImageView) convertView.findViewById(
R.id.thumbnail );
convertView.setTag( holder );
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText( getItem(position).getName() );//here too getName()
has null point exception
Picasso.with(getContext()).load( getItem( position ).getThumbnail()
).into( holder.thumbnail );// getThumbnail() too has null point
exception
return convertView;
}
private class ViewHolder {
ImageView thumbnail;
TextView name;
}
}
这是我的SuperHero.java类
import android.os.Parcel;
import android.os.Parcelable;
public class SuperHero implements Parcelable {
private String name;
private String description;
private String thumbnail;
private String image;
public SuperHero() {
}
public SuperHero( String name, String description, String thumbnail,
String image ) {
this.name = name;
this.description = description;
this.thumbnail = thumbnail;
this.image = image;
}
public SuperHero( Parcel source ) {
name = source.readString();
description = source.readString();
thumbnail = source.readString();
image = source.readString();
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
applyDefaultValues();
dest.writeString( name );
dest.writeString( description );
dest.writeString( thumbnail );
dest.writeString( image );
}
private void applyDefaultValues() {
if( name == null )
name = "";
if( description == null )
description = "";
if( thumbnail == null )
thumbnail = "";
if( image == null )
image = "";
}
public static Creator<SuperHero> CREATOR = new Creator<SuperHero>() {
@Override
public SuperHero createFromParcel(Parcel source) {
return new SuperHero( source );
}
@Override
public SuperHero[] newArray(int size) {
return new SuperHero[size];
}
};
}
我得到的错误如下 11-10 18:25:44.527 10738-10738 / com.example.abhilasha.marvelsfacts E / AndroidRuntime:FATAL EXCEPTION:main
Process: com.example.abhilasha.marvelsfacts, PID: 10738
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.abhilasha.marvelsfacts/
com.example.abhilasha.marvelsfacts.activities.ExhibitDetailActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String
com.example.abhilasha.marvelsfacts.models.SuperHero.getDescription()'
on a null object reference
at
android.app.ActivityThread.
performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String
com.example.abhilasha.marvelsfacts.models
.SuperHero.getDescription()' on a null object reference
at com.example.abhilasha.marvelsfacts.activities.ExhibitDetailActivity.onCreate(ExhibitDetailActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
.... 我无法得到究竟是什么异常以及如何解决方法异常任何建议请帮助... !!!!!!
这是activity_exhibit_detail.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/background_dark"/>
</LinearLayout>
</ScrollView>
这是我调用ExhibitDetailActivity.java的活动 ExhibitFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.example.abhilasha.marvelsfacts.R;
import
com.example.abhilasha.marvelsfacts.activities.ExhibitDetailActivity;
import
com.example.abhilasha.marvelsfacts.adapter.ExhibitsAdapter;
import com.example.abhilasha.marvelsfacts.models.SuperHero;
import
com.example.abhilasha.marvelsfacts.utils.SuperHeroApiInterface;
import java.util.List;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class ExhibitsListFragment extends ListFragment {
private ExhibitsAdapter mExhibitsAdaptern;
public static ExhibitsListFragment getInstance() {
ExhibitsListFragment fragment = new ExhibitsListFragment();
return fragment;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setListShown(false);
mExhibitsAdaptern=new ExhibitsAdapter(getActivity(),0);
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint( getString( R.string.super_hero ) )
.build();
SuperHeroApiInterface superHeroApiInterface =
restAdapter.create(SuperHeroApiInterface.class );
superHeroApiInterface.getStreams( new Callback<List<SuperHero>>()
{
@Override
public void success(List<SuperHero> superheroes, Response response)
{
if( superheroes == null || superheroes.isEmpty() )
return;
for( SuperHero superHero : superheroes ) {
mExhibitsAdaptern.add(superHero);
}
mExhibitsAdaptern.notifyDataSetChanged();
setListAdapter(mExhibitsAdaptern);
setListShown(true);
}
@Override
public void failure(RetrofitError error) {
Log.e( "Zoo", "Retrofit error " + error.getMessage() );
}
});
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent intent = new Intent( getActivity(), ExhibitDetailActivity.class
);
intent.putExtra( ExhibitDetailActivity.EXTRA_SUPERHERO,
mExhibitsAdaptern.getItem( position ) );
startActivity( intent );
}
}
答案 0 :(得分:0)
我认为问题不是getDescription()方法,而是 superhero 对象。 您刚刚声明了该对象,这意味着它不包含任何内容(null或垃圾)。 我认为如果为超级英雄对象分配内存,您的问题就会得到解决。 试试这个:
SuperHero superHero = new SuperHero();
答案 1 :(得分:-2)
更改此
SuperHero superHero=getIntent().getExtras().getParcelable("EXTRA_SUPERHERO");
到此:
SuperHero superHero=getIntent().getExtras().getParcelable(EXTRA_SUPERHERO);
您的密钥不同,这就是为什么superHero为空。希望它有所帮助:)