在Android应用中同时显示文本视图和按钮

时间:2014-06-03 22:22:31

标签: android android-layout android-linearlayout textview android-button

我对Android应用程序有点新意,正在制作一个简单的应用程序,使用意图在两个类之间发送数据。 (Android开发人员教程第一个应用程序https://developer.android.com/training/basics/firstapp/index.html)。我目前正在尝试添加按钮以转到另一个活动,同时在同一布局上显示文本视图。问题是,我似乎无法让按钮与textview同时显示。我尝试在下一个类的OnCreate方法中显示/隐藏按钮,现在尝试动态创建我想要的布局。我得到的问题是我的LinearLayout.addView(textView)选项,我得到一个空指针。任何人都可以就我的代码有什么问题提出任何建议,或者我是否应该废弃这个想法并一起尝试一个新的想法。我仍然很新,对所有建议持开放态度。谢谢!

活动

package com.example.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        //textView.setVisibility(backbutton.VISIBLE);
        // Set the text view as the activity layout
        setContentView(R.layout.fragment_main);
        LinearLayout rl = (LinearLayout)findViewById(R.id.relative_layout);
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(20);
        message = "The current address is " + message;
        textView.setText(message);
        Button backbutton;
        backbutton = (Button) this.findViewById(R.id.back);
        rl.addView(textView); //Null pointer exception HERE
        rl.addView(backbutton);
        setContentView(rl);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, 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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_display_message,
                    container, false);
            return rootView;
        }
    }

}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />
    <Button
        android:id="@+id/back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="Back"
        android:text="@string/back" />

</LinearLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" />
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.app.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="com.dm.zbar.android.scanner.ZBarScannerActivity"
        android:screenOrientation="landscape"
        android:label="@string/app_name" />
        <activity
        android:name="com.example.app.DisplayMessageActivity"
        android:label="@string/title_activity_display_address"
        android:parentActivityName="com.example.app.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
        <activity android:name="AddressChange"></activity>

    </application>

</manifest>

Fragment_main

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.app.MainActivity$PlaceholderFragment" >


</LinearLayout>

堆栈跟踪

06-03 22:30:25.940: E/AndroidRuntime(891): FATAL EXCEPTION: main
06-03 22:30:25.940: E/AndroidRuntime(891): Process: com.example.app, PID: 891
06-03 22:30:25.940: E/AndroidRuntime(891): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.DisplayMessageActivity}: java.lang.NullPointerException
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.os.Looper.loop(Looper.java:136)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.main(ActivityThread.java:5042)
06-03 22:30:25.940: E/AndroidRuntime(891):  at java.lang.reflect.Method.invokeNative(Native Method)
06-03 22:30:25.940: E/AndroidRuntime(891):  at java.lang.reflect.Method.invoke(Method.java:515)
06-03 22:30:25.940: E/AndroidRuntime(891):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
06-03 22:30:25.940: E/AndroidRuntime(891):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-03 22:30:25.940: E/AndroidRuntime(891):  at dalvik.system.NativeStart.main(Native Method)
06-03 22:30:25.940: E/AndroidRuntime(891): Caused by: java.lang.NullPointerException
06-03 22:30:25.940: E/AndroidRuntime(891):  at com.example.app.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:34)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.Activity.performCreate(Activity.java:5231)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-03 22:30:25.940: E/AndroidRuntime(891):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
06-03 22:30:25.940: E/AndroidRuntime(891):  ... 11 more

2 个答案:

答案 0 :(得分:2)

首先,您在活动中设置错误的布局(LinearLayout)findViewById(R.id.relative_layout);不在fragment_main中,它将返回null,因为它无法在您的布局中找到它。您可以使用位于不同布局的视图。

<强>溶液

使用XML File代替fragment_main

setContentView(R.layout.XML File);

删除:

Button backbutton;
    backbutton = (Button) this.findViewById(R.id.back); //already in the layout no need to add it

答案 1 :(得分:0)

除了罗德的修复,

将ID添加到:

    <TextView
    <--- add android:id="@+id/asdf"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
就像你按钮所做的一样。

然后使用ID获取文本视图,就像您对按钮所做的那样:

    TextView textView = new TextView(this);  <-- (TextView) this.findViewById(R.id.asdf)
    textView.setTextSize(20);
    <---- add textView.setText("Some text");

    Button backbutton;
    backbutton = (Button) this.findViewById(R.id.back);`