这是我的第3个应用程序,我认为我太野心勃勃了。一个应用程序向我显示带有一些名称的listview
,点击后会更改imageview
,此图片将从网址中检索。此外,我想屏幕幻灯片,它可以告诉我更多我刚才描述的“模板”。这是我遇到大多数问题的地方。
解决了 {
我的logcat说:
05-13 23:20:38.516: E/AndroidRuntime(687): java.lang.RuntimeException:
Unable to instantiate activity ComponentInfo{com.example.test2/
com.example.test2.ScreenSlideActivity}: java.lang.NullPointerException
}
新问题
点击listview
后,它会停止显示:
05-13 23:51:46.751: E/AndroidRuntime(782): java.lang.RuntimeException: An error occured while executing doInBackground()
05-13 23:51:46.751: E/AndroidRuntime(782): at android.os.AsyncTask$3.done(AsyncTask.java:299)
主要活动
public class ScreenSlideActivity extends ActionBarActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
return 6;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_screen_slide,
container, false);
String[] ponies = getResources().getStringArray(R.array.ponies);
String[] poniesurl=getResources().getStringArray(R.array.poniesimg);
final ArrayList<String> listurl = new ArrayList<String>();
ListView listview = (ListView) rootView.findViewById(R.id.lista);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < ponies.length; ++i) {
list.add(ponies[i]);
listurl.add(poniesurl[i]);
}
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<>(getActivity().getApplicationContext()
,android.R.layout.simple_list_item_1,list);
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedPonyURL=listurl.get(position);
new ScreenSlideActivity().new DownloadImagesTask().execute(selectedPonyURL);
}
});
return rootView;
}
}
class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... data) {
String thumb = data[0];
Bitmap bitmap = null;
try {
Log.d("TEST", "do in background");
bitmap = BitmapFactory
.decodeStream((InputStream) new URL(thumb).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap img) {
Log.d("TEST", "post execute");
ImageView mChart = (ImageView) findViewById(R.id.imageView1);
mChart.setImageBitmap(img);
}}
}
我的布局
activiy_screen_slide.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test2.ScreenSlideActivity" />
fragment_screen_slide.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageView1"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="200dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<ListView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1" >
</ListView>
</RelativeLayout>
和我的清单
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ponies">
<item>Rainbow Dash</item>
<item>Apple Jack</item>
<item>Rarity</item>
<item>Pinkie Pie</item>
<item>Twilight Sparkle</item>
<item>Fluttershy</item>
</string-array>
<array name="poniesimg">
<item>http://fc08.deviantart.net/fs71/f/2012/087/d/e/rainbow_dash_icon_request_by_megasuperheavy-d4u76ht.png</item>
<item>https://www.fimfiction-static.net/images/avatars/28886_256.jpg?1336013578</item>
<item>http://www.fimfiction-static.net/images/avatars/139363_256.jpg?1382473406</item>
<item>https://lh6.googleusercontent.com/-fPQ-TEfk9RE/AAAAAAAAAAI/AAAAAAAAAD4/W_oT9Nz5MpY/photo.jpg</item>
<item>https://pbs.twimg.com/profile_images/431214974363385856/GAmxwTe-.png</item>
<item>http://files.gamebanana.com/img/ico/sprays/515cc647f37a4.png</item>
</array>
</resources>
我知道这是一个弗兰肯斯坦,我真的很感激一些例子和建议。感谢
答案 0 :(得分:0)
问题在于:
new ScreenSlideActivity().new DownloadImagesTask()
正如您所看到的那样,您正在创建一个新的ScreenSlideActivity,并且AsyncTask属于这个新的,而不是当前正在执行的那个,这已经夸大了布局以及“找到”的位置。作品。因为您正在使用在新实例的实例上创建的AsyncTask,所以AsyncTask.onPostExecute()中的代码将严重失败:
ImageView mChart = (ImageView) findViewById(R.id.imageView1);
为什么呢?因为你不能执行findViewById(),因为你自己创建了Activity实例,而不是由Android管理。所以它丢失了。
所以不要使用这种方法...尝试将onItemClick执行委托给当前运行的Activity实例中的方法。就像这样:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedPonyURL=listurl.get(position);
((ScreenSlideActivity)getActivity()).handleOnItemClick(selectedPonyURL);
}
});
并且在内部你将把启动AsyncTask的处理放在同一个上下文中......就像你不会创建一个新的ScreenSlideActivity对象一样,你将重用现有的......在ScreenSlideActivity中: / p>
...
public void handleOnItemClick(String selectedPonyURL) {
new DownloadImagesTask().execute(selectedPonyURL);
}
...
注意:永远不要实例化一个Activity,它的创建由Android管理,如果你这样做,它就像一个普通的Java对象,但是有问题。