如何使用可滑动布局Android应用程序在应用程序中打开新屏幕?

时间:2014-08-21 22:15:59

标签: android android-layout android-activity

我已经在Android中创建了我的应用程序,但它不起作用 - 当我点击按钮时它没有在Android中打开我的应用程序的新屏幕,但它显示错误并结束我的应用程序。如果有人可以帮助我,并表明我做错了什么,我会很开心。这是我的代码: MainActivity

package pl.mylaw.mylaw2;
import pl.mylaw.mylaw2.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Cywilne", "Karne", "Admini...", "Inne" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}   
public void kpa_przejscie( View v)      
{
Intent intent = new Intent();
intent.setClass( this, KpaActivity.class);
startActivity( intent);
}
}

KpaActivty.class

package pl.mylaw.mylaw2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class KpaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kpa);
}
public void kpa_powrot( View v)
{
    finish();
}
}

和Manifest的一部分

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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="pl.mylaw.mylaw2.KpaActivity"
        android:label="@string/title_activity_kp" >
    </activity>
</application>

和按钮定义的最后一部分:     

08-22 00:49:26.558:E / AndroidRuntime(686):致命异常:主要 08-22 00:49:26.558:E / AndroidRuntime(686):java.lang.RuntimeException:无法启动活动ComponentInfo {pl.mylaw.mylaw2 / pl.mylaw.mylaw2.KpaActivity}:java.lang.RuntimeException:Binary XML文件行#6:您必须提供layout_width属性。 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread.access $ 600(ActivityThread.java:122) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.os.Handler.dispatchMessage(Handler.java:99) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.os.Looper.loop(Looper.java:137) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread.main(ActivityThread.java:4340) 08-22 00:49:26.558:E / AndroidRuntime(686):at java.lang.reflect.Method.invokeNative(Native Method) 08-22 00:49:26.558:E / AndroidRuntime(686):at java.lang.reflect.Method.invoke(Method.java:511) 08-22 00:49:26.558:E / AndroidRuntime(686):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 08-22 00:49:26.558:E / AndroidRuntime(686):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-22 00:49:26.558:E / AndroidRuntime(686):at dalvik.system.NativeStart.main(Native Method) 08-22 00:49:26.558:E / AndroidRuntime(686):引起:java.lang.RuntimeException:二进制XML文件行#6:您必须提供layout_width属性。 08-22 00:49:26.558:E / AndroidRuntime(686):在android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.ViewGroup $ LayoutParams.setBaseAttributes(ViewGroup.java:5297) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.ViewGroup $ MarginLayoutParams。(ViewGroup.java:5418) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.widget.RelativeLayout $ LayoutParams。(RelativeLayout.java:1064) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:933) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:69) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.LayoutInflater.rInflate(LayoutInflater.java:741) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.LayoutInflater.inflate(LayoutInflater.java:489) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.LayoutInflater.inflate(LayoutInflater.java:396) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.view.LayoutInflater.inflate(LayoutInflater.java:352) 08-22 00:49:26.558:E / AndroidRuntime(686):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.Activity.setContentView(Activity.java:1835) 08-22 00:49:26.558:E / AndroidRuntime(686):at pl.mylaw.mylaw2.KpaActivity.onCreate(KpaActivity.java:13) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.Activity.performCreate(Activity.java:4465) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 08-22 00:49:26.558:E / AndroidRuntime(686):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 08-22 00:49:26.558:E / AndroidRuntime(686):... 11更多

这里是LogCat错误:)

1 个答案:

答案 0 :(得分:0)

根据日志,似乎与KpaActivity类关联的某些xml属性没有属性&#34; layout_width&#34;。 &#34;您必须提供layout_width属性。&#34;

试试这段代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="KOdeks" />
</RelativeLayout>