我正在创建一个应用程序,我希望用户在纵向模式下拥有其他视图而不是横向模式。 所以我制作了一个普通的xml-fileen,然后制作了另一个我在xml文件制作时签名的格式模式。因此,在automaticcaly中创建了一个名为layout-land的额外文件夹,其中包含新的xml文件(与旧文件同名)。
但是每当我以横向模式启动我的应用程序时,它会崩溃(xml-file用于MainActivity.java)?
横向模式的我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/landscape1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Houd uw telefoon rechtop en herstart de app voor correcte weergave"
android:textColor="#665252" />
</RelativeLayout>
Logcat输出:
06-03 19:22:14.664: D/AndroidRuntime(285): Shutting down VM
06-03 19:22:14.664: W/dalvikvm(285): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-03 19:22:14.684: E/AndroidRuntime(285): FATAL EXCEPTION: main
06-03 19:22:14.684: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rome/com.example.rome.MainActivity}:
java.lang.NullPointerException
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.os.Looper.loop(Looper.java:123)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 19:22:14.684: E/AndroidRuntime(285): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:22:14.684: E/AndroidRuntime(285): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 19:22:14.684: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 19:22:14.684: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 19:22:14.684: E/AndroidRuntime(285): at dalvik.system.NativeStart.main(Native Method)
06-03 19:22:14.684: E/AndroidRuntime(285): Caused by: java.lang.NullPointerException
06-03 19:22:14.684: E/AndroidRuntime(285): at com.example.rome.MainActivity.onCreate(MainActivity.java:21)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 19:22:14.684: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-03 19:22:14.684: E/AndroidRuntime(285): ... 11 more
Mainactivity.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageButton1).setOnClickListener(this);
findViewById(R.id.imageButton2).setOnClickListener(this);
findViewById(R.id.imageButton3).setOnClickListener(this);
findViewById(R.id.imageButton4).setOnClickListener(this);
findViewById(R.id.imageButton5).setOnClickListener(this);
findViewById(R.id.imageButton6).setOnClickListener(this);
findViewById(R.id.imageButton7).setOnClickListener(this);
findViewById(R.id.imageButton8).setOnClickListener(this);
findViewById(R.id.imageButton9).setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.imageButton1:
startActivity(new Intent(MainActivity.this, Metros.class));
break;
case R.id.imageButton2:
startActivity(new Intent(MainActivity.this, Programma.class));
break;
case R.id.imageButton8:
startActivity(new Intent(MainActivity.this, Italiaans.class));
break;
//-- more cases --
case R.id.imageButton9:
startActivity(new Intent(MainActivity.this, Telefoonnummers.class));
break;
case R.id.imageButton7:
startActivity(new Intent(MainActivity.this, Kaart.class));
break;
case R.id.imageButton5:
startActivity(new Intent(MainActivity.this, Restaurants.class));
break;
case R.id.imageButton3:
startActivity(new Intent(MainActivity.this, Bus.class));
break;
case R.id.imageButton4:
startActivity(new Intent(MainActivity.this, Winkelen.class));
break;
case R.id.imageButton6:
startActivity(new Intent(MainActivity.this, Syllabus.class));
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Over:
startActivity(new Intent(this, Over.class));
return true;
case R.id.Hotels:
startActivity(new Intent(this, Hotels.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml(肖像模式版本):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0B0E15"
android:gravity="center_horizontal"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/metro2" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/imageButton1"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/programma" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_toRightOf="@+id/imageButton1"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/bus2" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:height="20dp"
android:textColor="#FFFFFF"
android:width="175dp" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_toLeftOf="@+id/imageButton3"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/supermarkt2" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_below="@+id/imageButton2"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/restaurants2" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton4"
android:layout_toRightOf="@+id/imageButton4"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/syllabus2" />
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton4"
android:layout_toLeftOf="@+id/imageButton6"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/kaartrome2" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton6"
android:layout_alignTop="@+id/imageButton7"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/italiaans2" />
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton7"
android:layout_toLeftOf="@+id/imageButton7"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/telefoon2" />
<TextView
android:id="@+id/Streeptop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="______________________________________________"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/Streep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Streeptop"
android:layout_below="@+id/imageButton7"
android:text="_______________________________"
android:textColor="#FFFFFF" />
</RelativeLayout>
你可以帮帮我吗?
提前致谢, 井
答案 0 :(得分:0)
来自Android开发者大会的以下视频显示了微观和宏观重排的示例(从大约11分钟开始)。 http://www.youtube.com/watch?v=Jl3-lzlzOJI
对于你想要的基本宏观回流(又名陆地与肖像):
- 文件中的纵向视图: RES /布局/ your_xml_file.xml
- 文件中的横向视图: RES /布局脊/ your_xml_file.xml
在两个单独的文件夹中有2个同名文件。您的xml代码似乎没有任何问题。
答案 1 :(得分:0)
问题在于,您为所有这些onClickListener
设置了Buttons
,这些Buttons
显然在景观定位中不存在。如果null
layout
未设置监听器或更好,请检查哪个Buttons
充气或设备位于哪个方向,您需要检查一下。
但是,我可以提供上述操作的示例,因为您对所有onClick
使用相同的侦听器,因此在xml纵向文件中设置Button
可能更容易。对于每个<Button
android:id="@+id/imageButton1"
android:onClick="functionName"
执行类似
onClick
然后在你的java代码中只需将onCreate()
命名为你在xml中使用的任何内容,而不必在implements View.OnClickListener
中设置这些监听器。所以你不需要findViewById(R.id.imageButton1).setOnClickListener(this);
并且可以取出所有的
Intents
This SO answer of mine可能有助于提高{{1}}效率。我喜欢它但可能适合或不适合你。