想实现两个桌面之间的跳转 但是运行的时候程序报错了!这个桌面是ImageButton组件做的,想实现按下按钮就可以跳转到这个页面
可是当程序运行的时候报错了!但是在logcat中报错是空指针!这个是主程序代码:
package com.IWeekly2;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;public class Main extends Activity {
  private ImageButton btnView,
  btnRead,
  btnPictrue,
  btnShopping,
  btnSending,
  btnSave,
  btnShare;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setupViewComponent();
    }
    
  
    
    private void setupViewComponent(){
     btnView = (ImageButton)findViewById(R.id.btnView);
     btnRead = (ImageButton)findViewById(R.id.btnRead);
     btnPictrue = (ImageButton)findViewById(R.id.btnPictrue);
     btnShopping = (ImageButton)findViewById(R.id.btnShopping);
     btnSending = (ImageButton)findViewById(R.id.btnSending);
     btnSave = (ImageButton)findViewById(R.id.btnSave);
     btnShare = (ImageButton)findViewById(R.id.btnShare);
    
     btnView.setOnClickListener(btnViewLis);
     btnRead.setOnClickListener(btnReadLis);
     btnPictrue.setOnClickListener(btnPictrueLis);
     btnShopping.setOnClickListener(btnShoppingLis);
     btnSending.setOnClickListener(btnSendingLis);
     btnSave.setOnClickListener(btnSaveLis);
     btnShare.setOnClickListener(btnShareLis);
    
    
   }
    
    private Button.OnClickListener btnViewLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
    
    private Button.OnClickListener btnReadLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
    
    private Button.OnClickListener btnPictrueLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
    
    private Button.OnClickListener btnShoppingLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
    
    private Button.OnClickListener btnSendingLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
    
    private Button.OnClickListener btnSaveLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }  
    };
    
    private Button.OnClickListener btnShareLis = new Button.OnClickListener(){
     public void onClick(View v){
     Intent v1 = new Intent();
     v1.setClass(Main.this,TabMain.class);
     startActivity(v1);
     }
    };
}
这个是AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.IWeekly2"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  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=".TabMain"/>
<activity android:name=".First"/>
<activity android:name=".Second"/>
<activity android:name=".Third"/>
<activity android:name=".Fourth"/>
<activity android:name=".Fifth"/>

    </application>
</manifest>
这个是出错的代码:
package com.IWeekly2;import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;public class TabMain extends TabActivity implements OnTouchListener, OnGestureListener {
private static final int FLING_MIN_DISTANCE = 20;
private static final int FLING_MIN_VELOCITY = 0;
TabHost tabHost;
GestureDetector mGestureDetector; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);

mGestureDetector = new GestureDetector(this);
LinearLayout ll=(LinearLayout)findViewById(R.id.linew);
ll.setOnTouchListener(this);
ll.setLongClickable(true);
tabHost = getTabHost();
tabHost.setup();
Intent intent1 = new Intent(TabMain.this, First.class);
createTab("消息", intent1);

Intent intent2 = new Intent(TabMain.this, Second.class);
createTab("聊天", intent2); Intent intent3 = new Intent(TabMain.this, Third.class);
createTab("通讯录", intent3); Intent intent4 = new Intent(TabMain.this, Fourth.class);
createTab("邮箱", intent4); Intent intent5 = new Intent(TabMain.this, Fifth.class);
createTab("单点登录", intent5);

TabWidget tabWidget = tabHost.getTabWidget();
int count = tabWidget.getChildCount(); tabHost.setCurrentTab(0);
} private void createTab(String text, Intent intent) {
tabHost.addTab(tabHost.newTabSpec(text).setIndicator(
createTabView(text)).setContent(intent));
} private View createTabView(String text) {
View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,
null);
TextView tv = (TextView) view.findViewById(R.id.tv_tab);
tv.setText(text);
return view;
} @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("touch","touch");
 return mGestureDetector.onTouchEvent(event); 
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int total=tabHost.getTabWidget().getChildCount();
int current=tabHost.getCurrentTab();
// TODO Auto-generated method stub
 if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE 
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 
            // Fling left 
            Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show(); 
            
            tabHost.setCurrentTab(current-1<0?0:current-1);
        } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE 
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 
            // Fling right 
          tabHost.setCurrentTab(current+1>total?total-1:current+1);
            Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show(); 
        } 
        return false; 
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}}
求大牛帮忙指点!谢谢了页面跳转Android

解决方案 »

  1.   

    setupViewComponent这个方法里有错
    具体第几行我不知道,你的图没截完整
      

  2.   

    你那些控件是在 R.layout.main 这个xml里面的吗
      

  3.   

    在啊!
    main.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/oo"
        >
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/yazhishishang"
    android:textSize="25sp"
    android:layout_marginLeft="100dp"
    android:layout_marginRight="100dp"
    android:layout_marginBottom="20dp"
    />
    <ImageButton android="@+id/btnView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/view"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnRead"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/read"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnPictrue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pictrue"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnShopping"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/shopping"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnSending"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sending"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/save"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    <ImageButton android="@+id/btnShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/share"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    </LinearLayout>
      

  4.   

    btnView.setOnClickListener(btnViewLis);
    这行出的错吧,换成View.OnClickListener吧,ImageButton不是Button的子类
      

  5.   

    Main类的setupViewComponent()函数的38行里有null值,仔细检查下,另外你的代码命名啥的太不规范了,建议你好好写写。
      

  6.   

    <ImageButton android="@+id/btnShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/share"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    />
    android:id="@+id/btnShare"定义写错了,改这个试试吧
    你定义xml都没有报错吗。。