亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么代碼出錯了啊

這是我的主文件
package?com.example.viewfilpper;


import?android.app.Activity;
import?android.os.Bundle;
import?android.widget.ImageView;
import?android.widget.ViewFlipper;


public?class?MainActivity?extends?Activity?{

	
	private?ViewFlipper?flipper;
	//將資源設置在一個數組當中
	private?int[]resID={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,};
	
????@Override
	protected?void?onCreate(Bundle?savedInstanceState)?{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
????????//初始化
????????flipper=(ViewFlipper)?findViewById(R.layout.main);
????????
????????
????????//用動態導入的方式為viewflipper加入子view
		for?(int?i?=?0;?i?<?resID.length;?i++)?{
			flipper.addView(getImageView(resID[i]));
		}
????????//添加動畫效果
????????flipper.setInAnimation(this,?R.anim.left_in);
????????flipper.setOutAnimation(this,?R.anim.left_out);
????????//設置視圖切換時間
????????flipper.setFlipInterval(1000);
????????flipper.startFlipping();
????}

????private?ImageView?getImageView(int?resID){
????	
????	ImageView?image=new?ImageView(this);
//????	image.setImageResource(resID);
????	image.setBackgroundResource(resID);//顯示鋪面屏幕的照片
????	return?image;
????	
????}
}

這是我的main函數

<?xml?version="1.0"?encoding="utf-8"?>
<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????android:layout_width="match_parent"
????android:layout_height="match_parent"
????android:orientation="vertical"?>
????
????<ViewFlipper?
????????android:layout_height="match_parent"
????????android:layout_width="match_parent"
????????android:id="@+id/flipper"
????????></ViewFlipper>
????

</LinearLayout>

這是log日志

08-06?11:14:07.668:?D/dalvikvm(5486):?GC_FOR_ALLOC?freed?100K,?5%?free?3059K/3220K,?paused?8ms,?total?8ms
08-06?11:14:07.672:?I/dalvikvm-heap(5486):?Grow?heap?(frag?case)?to?3.504MB?for?486012-byte?allocation
08-06?11:14:07.692:?D/dalvikvm(5486):?GC_FOR_ALLOC?freed?3K,?5%?free?3531K/3696K,?paused?18ms,?total?18ms
08-06?11:14:07.696:?D/AndroidRuntime(5486):?Shutting?down?VM
08-06?11:14:07.696:?W/dalvikvm(5486):?threadid=1:?thread?exiting?with?uncaught?exception?(group=0x94d6fb20)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?FATAL?EXCEPTION:?main
08-06?11:14:07.712:?E/AndroidRuntime(5486):?Process:?com.example.viewfilpper,?PID:?5486
08-06?11:14:07.712:?E/AndroidRuntime(5486):?java.lang.RuntimeException:?Unable?to?start?activity?ComponentInfo{com.example.viewfilpper/com.example.viewfilpper.MainActivity}:?java.lang.NullPointerException
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread.access$800(ActivityThread.java:135)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.os.Handler.dispatchMessage(Handler.java:102)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.os.Looper.loop(Looper.java:136)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread.main(ActivityThread.java:5045)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?java.lang.reflect.Method.invokeNative(Native?Method)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?java.lang.reflect.Method.invoke(Method.java:515)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?dalvik.system.NativeStart.main(Native?Method)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?Caused?by:?java.lang.NullPointerException
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?com.example.viewfilpper.MainActivity.onCreate(MainActivity.java:34)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.Activity.performCreate(Activity.java:5231)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	at?android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
08-06?11:14:07.712:?E/AndroidRuntime(5486):?	...?11?more

問題出在哪兒呢?謝謝

正在回答

2 回答

解決了就采納我吧,3積分別浪費了:D

0 回復 有任何疑惑可以回復我~
#1

差不多先生1234 提問者

非常感謝!
2016-08-13 回復 有任何疑惑可以回復我~

錯誤已經解決了,主要是因為??flipper=(ViewFlipper)?findViewById(R.layout.main);

應該是查找的main函數中的flipper函數,即:flipper=(ViewFlipper)findviewById(R.id.flipper);

1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android攻城獅的第二門課(第1季)
  • 參與學習       111145    人
  • 解答問題       1560    個

本課程由淺入深地帶您學會Android的常用控件的開發和使用

進入課程

為什么代碼出錯了啊

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號