Click here to Skip to main content
15,885,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i install this apk,the activity shows,but it doesn't execute any behind code.
I think there is something wrong in my AndroidMainfest.xml.But where?

this is my AndroidMainfest.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.notepad"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.notepad.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>

    </application>

</manifest>

this is activmainity_.xml:
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: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.notepad.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:inputType="text"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText"
        android:layout_alignBottom="@+id/editText"
        android:layout_alignParentRight="true"
        android:text="@string/addText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignRight="@+id/Add"
        android:layout_below="@+id/editText"
        android:layout_marginTop="43dp"
        android:text="@string/showText" />

</RelativeLayout>

this is MainActivity.java(behind behand)
Java
package com.example.notepad;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import android.annotation.SuppressLint;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class MainActivity extends ActionBarActivity {

	@SuppressLint("ShowToast")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Toast.makeText(this,".在创建时",1000);
		//为啥不行呢??????把这个注释了,拷代码
//		saveFile();
		
		Button iButton=(Button)findViewById(R.id.Add);
		iButton.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("ShowToast")
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), ".在点击时",1000);
				try {
					toSaveAndShow();
					Toast.makeText(getApplicationContext(),".在Try完成后",1000);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Toast.makeText(this, ".在新方法调用时",1000);
		File myFile=new File("data.txt");
		if(myFile.exists()!=true){
			try {
				myFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			//创建date对象记录时间
			Date myDate=new Date();
			//读取字符串。创建文件流。写入文件,关闭
			OutputStream out = new FileOutputStream(myFile,true);
			EditText youSay=(EditText)findViewById(R.id.editText);
			byte[] bufferByte=new byte[youSay.getText().length()+1];
			bufferByte=youSay.getText().toString().getBytes();
			out.write(bufferByte);
			String inStr="\t"+myDate.getDate();
			byte[] systemInfo=inStr.getBytes();
			out.write(systemInfo);
			out.close();
			//在textview中显示文本,病清空原来的文本
			TextView myView=(TextView)findViewById(R.id.textView);
			myView.setText(youSay.getText().toString()+inStr);
			youSay.setText("");
		Toast.makeText(this, ".我完成了",1000);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		Toast.makeText(this, ".出错了!",1000);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void saveFile(){
		Button iButton=(Button)findViewById(R.id.Add);
		iButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), ".在点击时",1000);
				try {
					toSaveAndShow();
					Toast.makeText(getApplicationContext(),".在Try完成后",1000);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

	}
	private void toSaveAndShow() throws IOException {
		// TODO Auto-generated method stub
				
		
		Toast.makeText(this, ".在新方法调用时",1000);
		File myFile=new File("data.txt");
		if(myFile.exists()!=true){
			myFile.createNewFile();
		}
		try {
			//创建date对象记录时间
			Date myDate=new Date();
			//读取字符串。创建文件流。写入文件,关闭
			OutputStream out = new FileOutputStream(myFile,true);
			EditText youSay=(EditText)findViewById(R.id.editText);
			byte[] bufferByte=new byte[youSay.getText().length()+1];
			bufferByte=youSay.getText().toString().getBytes();
			out.write(bufferByte);
			String inStr="\t"+myDate.getDate();
			byte[] systemInfo=inStr.getBytes();
			out.write(systemInfo);
			out.close();
			//在textview中显示文本,病清空原来的文本
			TextView myView=(TextView)findViewById(R.id.textView);
			myView.setText(youSay.getText().toString()+inStr);
			youSay.setText("");
		Toast.makeText(this, ".我完成了",1000);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		Toast.makeText(this, ".出错了!",1000);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

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

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900