当我运行我的应用程序时,我遇到了这些错误,我试图用RSS创建应用程序
Photo2.java
package com.otticafotobenzi.ofbshop;
import com.otticafotobenzi.R;
public class Photo2 extends ActionBarActivity implements View.OnClickListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper;
private Context mContext;
Button buttonCan;
Button buttonFuj;
Button buttonNik;
Button buttonOly;
Button buttonPana;
Button buttonPen;
Button buttonSon;
@SuppressWarnings("deprecation")
private final GestureDetector detector = new GestureDetector(
new SwipeGestureDetector());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo2);
buttonCan = (Button) findViewById(R.id.buttonCan);
buttonFuj = (Button) findViewById(R.id.buttonFuj);
buttonNik = (Button) findViewById(R.id.buttonNik);
buttonOly = (Button) findViewById(R.id.buttonOly);
buttonPana = (Button) findViewById(R.id.buttonPana);
buttonPen = (Button) findViewById(R.id.buttonPen);
buttonSon = (Button) findViewById(R.id.buttonSon);
buttonCan.setOnClickListener(this);
buttonFuj.setOnClickListener(this);
buttonNik.setOnClickListener(this);
buttonOly.setOnClickListener(this);
buttonPana.setOnClickListener(this);
buttonPen.setOnClickListener(this);
buttonSon.setOnClickListener(this);
mContext = this;
mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
mViewFlipper.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonCan:
Intent loader_rssnik = new Intent(this, RssService.class); //problem here
startActivity(loader_rssnik);
break;
case R.id.buttonFuj:
Intent loader_rsscan = new Intent(this, RssService.class);
startActivity(loader_rsscan);
break;
case R.id.buttonNik:
Intent loader_rssfuji = new Intent(this, RssServiceNik.class);
startActivity(loader_rssfuji);
break;
case R.id.buttonOly:
Intent loader_rssoly = new Intent(this, RssService.class);
startActivity(loader_rssoly);
break;
case R.id.buttonPana:
Intent loader_rsspen = new Intent(this, RssService.class);
startActivity(loader_rsspen);
break;
case R.id.buttonPen:
Intent loader_rsspan = new Intent(this, RssService.class);
startActivity(loader_rsspan);
break;
case R.id.buttonSon:
Intent loader_rssson = new Intent(this, RssService.class);
startActivity(loader_rssson);
break;
}
}
class SwipeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
mContext, R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
mContext, R.anim.left_out));
mViewFlipper.showNext();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(
mContext, R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
mContext, R.anim.right_out));
mViewFlipper.showPrevious();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.photography, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.otticafotobenzi"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".ofbshop.Loader"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ofbshop.Home"
android:label="@string/title_activity_home">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ofbshop.Photography"
android:label="@string/title_activity_photography">
</activity>
<activity android:name=".ofbshop.Photo2"
android:label="@string/title_activity_photography">
</activity>
<activity android:name=".ofbshop.Optics"
android:label="@string/title_activity_optics">
</activity>
<activity android:name=".rss.Constants" />
<activity android:name=".rss.MainActivity" />
<activity android:name=".rss.RssAdapter" />
<activity android:name=".rss.RssFragment" />
<activity android:name=".rss.RssItem" />
<activity android:name=".rss.RssParser" />
<activity android:name=".rss.RssService" />
<activity android:name=".rss.RssServiceNik" />
</application>
RssService.java
package com.otticafotobenzi.rss;
public class RssService extends IntentService {
private static final String RSS_LINK = "http://feeds.feedburner.com/PhotoRumors?format=xml";
public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";
public RssService() {
super("RssService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(Constants.TAG, "Service started");
List<RssItem> rssItems = null;
try {
RssParser parser = new RssParser();
rssItems = parser.parse(getInputStream(RSS_LINK));
} catch (XmlPullParserException e) {
Log.w(e.getMessage(), e);
} catch (IOException e) {
Log.w(e.getMessage(), e);
}
Bundle bundle = new Bundle();
bundle.putSerializable(ITEMS, (Serializable) rssItems);
ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
receiver.send(0, bundle);
}
public InputStream getInputStream(String link) {
try {
URL url = new URL(link);
return url.openConnection().getInputStream();
} catch (IOException e) {
Log.w(Constants.TAG, "Exception while retrieving the input stream",
e);
return null;
}
}
}
这是使用genymotion模拟器android 4.4
的logcat中的错误02-12 17:04:50.552: E/AndroidRuntime(2044): FATAL EXCEPTION: main
02-12 17:04:50.552: E/AndroidRuntime(2044): Process: com.otticafotobenzi, PID: 2044
02-12 17:04:50.552: E/AndroidRuntime(2044): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.otticafotobenzi/com.otticafotobenzi.rss.RssService}: java.lang.ClassCastException: com.otticafotobenzi.rss.RssService cannot be cast to android.app.Activity
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.os.Looper.loop(Looper.java:136)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread.main(ActivityThread.java:5001)
02-12 17:04:50.552: E/AndroidRuntime(2044): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:04:50.552: E/AndroidRuntime(2044): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 17:04:50.552: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-12 17:04:50.552: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-12 17:04:50.552: E/AndroidRuntime(2044): at dalvik.system.NativeStart.main(Native Method)
02-12 17:04:50.552: E/AndroidRuntime(2044): Caused by: java.lang.ClassCastException: com.otticafotobenzi.rss.RssService cannot be cast to android.app.Activity
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-12 17:04:50.552: E/AndroidRuntime(2044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
02-12 17:04:50.552: E/AndroidRuntime(2044): ... 11 more
答案 0 :(得分:0)
您需要为服务调用另一种方法:
Intent loader_rssnik = new Intent(this, RssService.class);
startService(loader_rssfuji);
答案 1 :(得分:0)
更改
<activity android:name=".rss.RssService" />
到
<service android:name=".rss.RssService" />
如果你想启动服务使用方法
startService(Intent);
通过
startActivity(Intent);